
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>范例教学 · python 学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="snowdreams1006">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="exercise.html" />
    
    
    <link rel="prev" href="about.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"snowdreams1006","repo":"learn-python","type":"star","size":"small"}]};
    </script>

    
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    
    
        <link rel="bookmark" href="/bookmark.ico" type="image/x-icon">
    
    
        <link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152.png">
    
    
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="/apple-touch-icon-precomposed-152.png">
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120.png">
        
        <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon-180.png">
        
    
    
        
        <link rel="apple-touch-icon-precomposed" sizes="120x120" href="/apple-touch-icon-precomposed-120.png">
        
        <link rel="apple-touch-icon-precomposed" sizes="180x180" href="/apple-touch-icon-precomposed-180.png">
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://snowdreams1006.tech/" target="_blank" class="custom-link">blog</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../env/about.html">
            
                <a href="../../env/about.html">
            
                    
                    环境准备
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../env/download.html">
            
                <a href="../../env/download.html">
            
                    
                    下载安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../env/ide.html">
            
                <a href="../../env/ide.html">
            
                    
                    开发工具
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../quickstart/about.html">
            
                <a href="../../quickstart/about.html">
            
                    
                    快速入门
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../quickstart/type.html">
            
                <a href="../../quickstart/type.html">
            
                    
                    数据类型
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../about.html">
            
                <a href="../about.html">
            
                    
                    网络请求
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="about.html">
            
                <a href="about.html">
            
                    
                    原生请求
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.4.1.1" data-path="teaching.html">
            
                <a href="teaching.html">
            
                    
                    范例教学
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.1.2" data-path="exercise.html">
            
                <a href="exercise.html">
            
                    
                    实例练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../requests/about.html">
            
                <a href="../requests/about.html">
            
                    
                    第三方请求
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.2.1" data-path="../requests/teaching.html">
            
                <a href="../requests/teaching.html">
            
                    
                    范例教学
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.2" data-path="../requests/exercise.html">
            
                <a href="../requests/exercise.html">
            
                    
                    实例练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>

    
    <li>
        
        <a href="http://www.beian.miit.gov.cn/" target="blank" class="gitbook-link" style="color:#f72b07">
        浙ICP备18042346号
        </a>
    </li>
    
</ul>

                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >范例教学</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                
                <div id="vip-container">
                    <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x8303;&#x4F8B;&#x6559;&#x5B66;"><b>1. </b>&#x8303;&#x4F8B;&#x6559;&#x5B66;</a></li><ul><li><span class="title-icon "></span><a href="#&#x73AF;&#x5883;&#x642D;&#x5EFA;"><b>1.1. </b>&#x73AF;&#x5883;&#x642D;&#x5EFA;</a></li><ul><li><span class="title-icon "></span><a href="#&#x73AF;&#x5883;&#x6F14;&#x793A;"><b>1.1.1. </b>&#x73AF;&#x5883;&#x6F14;&#x793A;</a></li><li><span class="title-icon "></span><a href="#&#x73AF;&#x5883;&#x642D;&#x5EFA;_1"><b>1.1.2. </b>&#x73AF;&#x5883;&#x642D;&#x5EFA;</a></li></ul><li><span class="title-icon "></span><a href="#&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;-urllib-&#x5E93;"><b>1.2. </b>&#x7F51;&#x7EDC;&#x8BF7;&#x6C42; urllib &#x5E93;</a></li><ul><li><span class="title-icon "></span><a href="#&#x600E;&#x4E48;&#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;"><b>1.2.1. </b>&#x600E;&#x4E48;&#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;</a></li><li><span class="title-icon "></span><a href="#&#x600E;&#x4E48;&#x77E5;&#x9053;&#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;"><b>1.2.2. </b>&#x600E;&#x4E48;&#x77E5;&#x9053;&#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A;-get-&#x8BF7;&#x6C42;"><b>1.2.3. </b>&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A; GET &#x8BF7;&#x6C42;</a></li><li><span class="title-icon "></span><a href="#&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A;-post-&#x8BF7;&#x6C42;"><b>1.2.4. </b>&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A; POST &#x8BF7;&#x6C42;</a></li><li><span class="title-icon "></span><a href="#&#x5982;&#x4F55;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;&#x8FDB;&#x884C;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;"><b>1.2.5. </b>&#x5982;&#x4F55;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;&#x8FDB;&#x884C;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;</a></li></ul><li><span class="title-icon "></span><a href="#&#x5B66;&#x4E60;&#x603B;&#x7ED3;"><b>1.3. </b>&#x5B66;&#x4E60;&#x603B;&#x7ED3;</a></li><ul><li><span class="title-icon "></span><a href="#&#x865A;&#x62DF;&#x73AF;&#x5883;-virtualenv"><b>1.3.1. </b>&#x865A;&#x62DF;&#x73AF;&#x5883; virtualenv</a></li><li><span class="title-icon "></span><a href="#&#x670D;&#x52A1;&#x7AEF;&#x540E;&#x53F0;-httpbin"><b>1.3.2. </b>&#x670D;&#x52A1;&#x7AEF;&#x540E;&#x53F0; httpbin</a></li><li><span class="title-icon "></span><a href="#&#x514D;&#x8D39;ip&#x4EE3;&#x7406;&#x6C60;-proxyip"><b>1.3.3. </b>&#x514D;&#x8D39;ip&#x4EE3;&#x7406;&#x6C60; proxyip</a></li><li><span class="title-icon "></span><a href="#&#x539F;&#x751F;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;-urllib"><b>1.3.4. </b>&#x539F;&#x751F;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42; urllib</a></li></ul><li><span class="title-icon "></span><a href="#&#x53C2;&#x8003;&#x6587;&#x6863;"><b>1.4. </b>&#x53C2;&#x8003;&#x6587;&#x6863;</a></li></ul></ul></div><a href="#&#x8303;&#x4F8B;&#x6559;&#x5B66;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="&#x8303;&#x4F8B;&#x6559;&#x5B66;"><a name="&#x8303;&#x4F8B;&#x6559;&#x5B66;" class="anchor-navigation-ex-anchor" href="#&#x8303;&#x4F8B;&#x6559;&#x5B66;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x8303;&#x4F8B;&#x6559;&#x5B66;</h1>
<blockquote>
<p><code>urllib.urlopen(url[,data[,proxies]])</code> : <a href="https://docs.python.org/2/library/urllib.html" target="_blank">https://docs.python.org/2/library/urllib.html</a></p>
</blockquote>
<p><code>python</code> &#x4E2D;&#x9ED8;&#x8BA4;&#x81EA;&#x5E26;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x7684;&#x5E93;&#x5C31;&#x662F; <code>urlllib</code> &#x7CFB;&#x5217;&#x4E86;,&#x5305;&#x62EC; <code>urllib</code> <code>urllib2</code> &#x4EE5;&#x53CA; <code>urllib3</code> ,&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#x4E09;&#x8005;&#x90FD;&#x662F;&#x4E92;&#x76F8;&#x914D;&#x5408;&#x4E00;&#x8D77;&#x5408;&#x4F5C;.</p>
<p>&#x5F53;&#x7136;&#x4E5F;&#x6709;&#x4E0D;&#x5C11;&#x4F18;&#x79C0;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x5E93;&#x53D1;&#x9001;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;,&#x6700;&#x4E3A;&#x5927;&#x4F17;&#x719F;&#x77E5;&#x7684;&#x5E94;&#x8BE5;&#x5305;&#x62EC; <code>requests</code> &#x5E93;,&#x4E0D;&#x8FC7;&#x672C;&#x6587;&#x6253;&#x7B97;&#x4ECE;&#x6700;&#x57FA;&#x7840;&#x7684; <code>urllib</code> &#x5F00;&#x59CB;&#x8BB2;&#x8D77;&#x5982;&#x4F55;&#x53D1;&#x9001;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;,&#x4E0B;&#x9762;&#x8BF7;&#x8BFB;&#x8005;&#x8DDF;&#x7740;<a href="https://snowdreams1006.github.io/learn-python/" target="_blank">&#x96EA;&#x4E4B;&#x68A6;&#x6280;&#x672F;&#x9A7F;&#x7AD9;</a>&#x7684;&#x601D;&#x8DEF;&#x4E00;&#x8D77;&#x52A8;&#x624B;&#x5B9E;&#x8DF5;&#x5427;!</p>
<ul>
<li><code>Github</code> &#x6E90;&#x7801;&#x5730;&#x5740;: <a href="https://github.com/snowdreams1006/learn-python/blob/master/src/url/urllib/urllib_demo.py" target="_blank">https://github.com/snowdreams1006/learn-python/blob/master/src/url/urllib/urllib_demo.py</a></li>
<li><code>Github</code> &#x6587;&#x6863;&#x5730;&#x5740;: <a href="https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html" target="_blank">https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html</a></li>
</ul>
<blockquote>
<p>&#x300C;&#x96EA;&#x4E4B;&#x68A6;&#x6280;&#x672F;&#x9A7F;&#x7AD9;&#x300D;&#x63D0;&#x9192;&#x60A8;: &#x7531;&#x4E8E;&#x6587;&#x7AE0;&#x5177;&#x6709;&#x4E00;&#x5B9A;&#x7684;&#x65F6;&#x6548;&#x6027;,&#x5F88;&#x6709;&#x53EF;&#x80FD;&#x8BFB;&#x8005;&#x6B63;&#x5728;&#x9605;&#x8BFB;&#x65F6;,&#x90E8;&#x5206;&#x94FE;&#x63A5;&#x5DF2;&#x5931;&#x6548;,&#x8BF7;&#x6309;&#x7167;&#x6587;&#x7AE0;&#x76F8;&#x5173;&#x8BF4;&#x660E;&#x4EB2;&#x81EA;&#x5B9E;&#x8DF5;&#x9A8C;&#x8BC1;,<strong>&#x5C3D;&#x4FE1;&#x4E66;&#x4E0D;&#x5982;&#x65E0;&#x4E66;</strong>,&#x4E0D;&#x8981;&#x76F4;&#x63A5;&#x590D;&#x5236;&#x7C98;&#x8D34;&#x4EE3;&#x7801;,<strong>&#x4E00;&#x5B9A;&#x8981;&#x81EA;&#x5DF1;&#x52A8;&#x624B;&#x4EB2;&#x81EA;&#x6572;&#x4E00;&#x904D;</strong>!</p>
</blockquote>
<h2 id="&#x73AF;&#x5883;&#x642D;&#x5EFA;"><a name="&#x73AF;&#x5883;&#x642D;&#x5EFA;" class="anchor-navigation-ex-anchor" href="#&#x73AF;&#x5883;&#x642D;&#x5EFA;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. &#x73AF;&#x5883;&#x642D;&#x5EFA;</h2>
<p>&#x672C;&#x6587;&#x4F7F;&#x7528;&#x7684; <code>python</code> &#x73AF;&#x5883;&#x662F;&#x57FA;&#x4E8E; <code>virtualenv</code> &#x5B9E;&#x73B0;&#x7684;&#x865A;&#x62DF;&#x73AF;&#x5883;,&#x53EA;&#x662F;&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x9694;&#x79BB;&#x4E0D;&#x540C;&#x73AF;&#x5883;,&#x66F4;&#x597D;&#x6A21;&#x62DF;&#x771F;&#x5B9E;&#x7528;&#x6237;&#x73AF;&#x5883;.</p>
<p>&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;,&#x53EF;&#x4EE5;&#x8DDF;&#x7740;&#x672C;&#x6587;&#x4E00;&#x6B65;&#x4E00;&#x6B65;&#x64CD;&#x4F5C;&#x7EC3;&#x4E60;&#x642D;&#x5EFA;&#x5F00;&#x53D1;&#x73AF;&#x5883;,&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x73AF;&#x5883;.</p>
<h3 id="&#x73AF;&#x5883;&#x6F14;&#x793A;"><a name="&#x73AF;&#x5883;&#x6F14;&#x793A;" class="anchor-navigation-ex-anchor" href="#&#x73AF;&#x5883;&#x6F14;&#x793A;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.1. &#x73AF;&#x5883;&#x6F14;&#x793A;</h3>
<p>&#x6F14;&#x793A;&#x73AF;&#x5883;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-bash">(.env)$ python --version
Python 2.7.16
(.env) $ pip --version
pip 19.3.1 from ~/python/src/url/urllib/.env/lib/python2.7/site-packages/pip (python 2.7)
</code></pre>
<blockquote>
<p>&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x5728;&#x8BE5;&#x73AF;&#x5883;&#x8FD0;&#x884C;&#x6B63;&#x5E38;,&#x4F46;&#x662F;&#x5E76;&#x4E0D;&#x4FDD;&#x8BC1;&#x5176;&#x4ED6;&#x73AF;&#x5883;&#x4E0E;&#x6F14;&#x793A;&#x7ED3;&#x679C;&#x4E00;&#x81F4;,&#x6240;&#x4EE5;&#x4E00;&#x5207;&#x8FD8;&#x662F;&#x4EE5;&#x5B9E;&#x9645;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#x51C6;.</p>
</blockquote>
<h3 id="&#x73AF;&#x5883;&#x642D;&#x5EFA;_1"><a name="&#x73AF;&#x5883;&#x642D;&#x5EFA;_1" class="anchor-navigation-ex-anchor" href="#&#x73AF;&#x5883;&#x642D;&#x5EFA;_1"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.2. &#x73AF;&#x5883;&#x642D;&#x5EFA;</h3>
<p>&#x5982;&#x679C;&#x4E0D;&#x9700;&#x8981;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x7684;&#x8BDD;,&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x73AF;&#x5883;&#x5B89;&#x88C5;&#x8FD9;&#x4E00;&#x90E8;&#x5206;&#x5185;&#x5BB9;,&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x73AF;&#x5883;&#x5373;&#x53EF;,&#x53EA;&#x9700;&#x8981;&#x4FDD;&#x8BC1;&#x6D4B;&#x8BD5;&#x65F6;&#x4F7F;&#x7528;&#x7684; <code>python</code> &#x7248;&#x672C;&#x662F; <code>python2</code> &#x800C;&#x975E; <code>python3</code>!</p>
<ul>
<li>&#x6B65;&#x9AA4;1. &#x5B89;&#x88C5;&#x865A;&#x62DF;&#x73AF;&#x5883; <code>virtualenv</code> </li>
</ul>
<pre><code class="lang-bash">sudo pip install virtualenv
</code></pre>
<blockquote>
<p>&#x5B89;&#x88C5;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x65B9;&#x4FBF;&#x9694;&#x79BB;&#x4E0D;&#x540C; python &#x73AF;&#x5883;,&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x73AF;&#x5883;</strong>,&#x6240;&#x4EE5;&#x8FD9;&#x4E00;&#x6B65;&#x662F;&#x53EF;&#x9009;&#x7684;,&#x540C;&#x7406;&#x4E0B;&#x9762;&#x7684;&#x6B65;&#x9AA4;&#x4E5F;&#x90FD;&#x662F;&#x53EF;&#x9009;&#x7684;.</p>
</blockquote>
<ul>
<li>&#x6B65;&#x9AA4;2. &#x51C6;&#x5907;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x76EE;&#x5F55; <code>.env</code></li>
</ul>
<pre><code class="lang-bash">virtualenv .env
</code></pre>
<blockquote>
<p>&#x865A;&#x62DF;&#x73AF;&#x5883;&#x76EE;&#x5F55;&#x8BBE;&#x7F6E;&#x6210;&#x9690;&#x85CF;&#x76EE;&#x5F55;&#x7684;&#x76EE;&#x7684;&#x662F;&#x9632;&#x6B62;&#x8BEF;&#x64CD;&#x4F5C;,&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x6210;&#x666E;&#x901A;&#x76EE;&#x5F55;&#x90A3;&#x6837;&#x663E;&#x793A;&#x51FA;&#x6765;.</p>
</blockquote>
<ul>
<li>&#x6B65;&#x9AA4;3. &#x6FC0;&#x6D3B;&#x865A;&#x62DF;&#x73AF;&#x5883; <code>.env</code></li>
</ul>
<pre><code class="lang-bash"><span class="hljs-built_in">source</span> .env/bin/activate
</code></pre>
<blockquote>
<p>&#x4E00;&#x65E6;&#x51C6;&#x5907;&#x597D;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x76EE;&#x5F55;&#x540E;&#x5C31;&#x9700;&#x8981;&#x6FC0;&#x6D3B;&#x8BE5;&#x865A;&#x62DF;&#x73AF;&#x5883;,&#x8FD9;&#x4E00;&#x6B65;&#x53EF;&#x4EE5;&#x91CD;&#x590D;&#x64CD;&#x4F5C;,&#x4E0D;&#x4F1A;&#x62A5;&#x9519;!</p>
</blockquote>
<ul>
<li>&#x6B65;&#x9AA4;4. &#x67E5;&#x770B;&#x5F53;&#x524D;&#x6B63;&#x5728;&#x4F7F;&#x7528;&#x7684; <code>python</code> &#x4E0E; <code>pip</code> &#x7248;&#x672C;</li>
</ul>
<pre><code class="lang-bash">(.env) $ <span class="hljs-built_in">which</span> python
~/python/src/url/urllib/.env/bin/python
(.env) snowdreams1006s-MacBook-Pro:urllib snowdreams1006$ <span class="hljs-built_in">which</span> pip
~/python/src/url/urllib/.env/bin/pip
</code></pre>
<blockquote>
<p>&#x6FC0;&#x6D3B;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x540E;&#x4F1A;&#x81EA;&#x52A8;&#x4E0B;&#x8F7D;&#x76F8;&#x5173;&#x7684; <code>python</code> &#x4F9D;&#x8D56;,&#x56E0;&#x6B64; <code>python</code> &#x548C; <code>pip</code> &#x6587;&#x4EF6;&#x4F4D;&#x7F6E;&#x6B63;&#x662F;&#x5F53;&#x524D;&#x76EE;&#x5F55; <code>.env</code> &#x800C;&#x4E0D;&#x662F;&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x73AF;&#x5883;,&#x5982;&#x679C;&#x672A;&#x5F00;&#x542F;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x5219;&#x663E;&#x793A;&#x7684;&#x662F;&#x7CFB;&#x7EDF;&#x76EE;&#x5F55;.</p>
</blockquote>
<h2 id="&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;-urllib-&#x5E93;"><a name="&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;-urllib-&#x5E93;" class="anchor-navigation-ex-anchor" href="#&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;-urllib-&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. &#x7F51;&#x7EDC;&#x8BF7;&#x6C42; urllib &#x5E93;</h2>
<p>&#x5982;&#x679C;&#x8BFB;&#x8005;&#x4EB2;&#x6D4B;&#x8FD0;&#x884C;&#x65F6;&#x53D1;&#x73B0;&#x7F51;&#x7EDC;&#x65E0;&#x6CD5;&#x6B63;&#x5E38;&#x8BF7;&#x6C42;,&#x53EF;&#x4EE5;&#x5C06; <a href="http://httpbin.snowdreams1006.cn/" target="_blank">http://httpbin.snowdreams1006.cn/ </a> &#x66FF;&#x6362;&#x6210; <a href="http://httpbin.org/" target="_blank">http://httpbin.org/</a> &#x6216;&#x8005;&#x81EA;&#x884C;&#x642D;&#x5EFA;&#x672C;&#x5730;&#x6D4B;&#x8BD5;&#x73AF;&#x5883;.</p>
<p>&#x4E0B;&#x9762;&#x63D0;&#x4F9B;&#x4E24;&#x79CD;&#x642D;&#x5EFA;&#x672C;&#x5730;&#x6D4B;&#x8BD5;&#x73AF;&#x5883;&#x7684;&#x5B89;&#x88C5;&#x65B9;&#x5F0F;,&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE; <a href="http://httpbin.snowdreams1006.cn/" target="_blank">http://httpbin.snowdreams1006.cn/</a> &#x6216;&#x8005; <a href="http://httpbin.org/" target="_blank">http://httpbin.org/</a> &#x7B49;&#x5728;&#x7EBF;&#x73AF;&#x5883;.</p>
<ul>
<li><code>docker</code> &#x5B89;&#x88C5;&#x65B9;&#x5F0F;</li>
</ul>
<p><img src="images/url-urllib-httpbin-docker-preview.png" alt="url-urllib-httpbin-docker-preview.png"></p>
<pre><code class="lang-bash">docker run -p 8000:80 kennethreitz/httpbin
</code></pre>
<blockquote>
<p>&#x9996;&#x6B21;&#x8FD0;&#x884C;&#x4F1A;&#x5148;&#x5C06;&#x955C;&#x50CF;&#x4E0B;&#x8F7D;&#x5230;&#x672C;&#x5730;&#x518D;&#x542F;&#x52A8;&#x5BB9;&#x5668;,&#x975E;&#x9996;&#x6B21;&#x8FD0;&#x884C;&#x4F1A;&#x76F4;&#x63A5;&#x542F;&#x52A8;&#x5BB9;&#x5668;,&#x8BBF;&#x95EE;&#x5730;&#x5740;: <a href="http://127.0.0.1:8000/" target="_blank">http://127.0.0.1:8000/</a></p>
</blockquote>
<ul>
<li><code>python</code> &#x5B89;&#x88C5;&#x65B9;&#x5F0F;</li>
</ul>
<p><img src="images/url-urllib-httpbin-pip-preview.png" alt="url-urllib-httpbin-pip-preview.png"></p>
<pre><code class="lang-bash">pip install gunicorn httpbin &amp;&amp; gunicorn httpbin:app
</code></pre>
<blockquote>
<p>&#x9ED8;&#x8BA4;&#x76D1;&#x542C;&#x7AEF;&#x53E3;&#x662F; <code>8000</code>,&#x5982;&#x679C;&#x9047;&#x5230;&#x7AEF;&#x53E3;&#x51B2;&#x7A81;&#x63D0;&#x793A;&#x5DF2;&#x88AB;&#x5360;&#x7528;,&#x53EF;&#x8FD0;&#x884C; <code>gunicorn httpbin:app -b :9898</code> &#x6307;&#x5B9A;&#x7AEF;&#x53E3;.</p>
</blockquote>
<h3 id="&#x600E;&#x4E48;&#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;"><a name="&#x600E;&#x4E48;&#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;" class="anchor-navigation-ex-anchor" href="#&#x600E;&#x4E48;&#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.1. &#x600E;&#x4E48;&#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;</h3>
<blockquote>
<p><code>urllib2.urlopen(url)</code> : &#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;,&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x54CD;&#x5E94;&#x4F53;&#x6587;&#x672C;&#x6570;&#x636E;.</p>
</blockquote>
<p>&#x65B0;&#x5EFA; <code>python</code> &#x6587;&#x4EF6;&#x540D;&#x4E3A; <code>urllib_demo.py</code>,&#x6838;&#x5FC3;&#x4EE3;&#x7801;&#x5305;&#x62EC;&#x5148;&#x5BFC;&#x5165; <code>urllib2</code> &#x5305;,&#x7136;&#x540E;&#x4F7F;&#x7528; <code>urllib2.urlopen()</code> &#x5373;&#x53EF;&#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684; <code>GET</code> &#x8BF7;&#x6C42;,&#x6700;&#x540E;&#x5229;&#x7528; <code>response.read()</code> &#x53EF;<strong>&#x4E00;&#x6B21;&#x6027;&#x8BFB;&#x53D6;</strong>&#x54CD;&#x5E94;&#x4F53;&#x5185;&#x5BB9;.</p>
<p>&#x4EE3;&#x7801;&#x5185;&#x5BB9;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> response.read()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<blockquote>
<p>&#x5047;&#x5982;&#x8BE5;&#x6587;&#x4EF6;&#x540D;&#x4E3A; <code>urllib_demo.py</code> ,&#x5219;&#x5728;&#x7EC8;&#x7AEF;&#x547D;&#x4EE4;&#x884C;&#x5185;&#x8FD0;&#x884C; <code>python urllib_demo.py</code> &#x5373;&#x53EF;&#x67E5;&#x770B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;.</p>
</blockquote>
<h3 id="&#x600E;&#x4E48;&#x77E5;&#x9053;&#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;"><a name="&#x600E;&#x4E48;&#x77E5;&#x9053;&#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x600E;&#x4E48;&#x77E5;&#x9053;&#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.2. &#x600E;&#x4E48;&#x77E5;&#x9053;&#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</h3>
<blockquote>
<p><code>print type(response)</code> : &#x83B7;&#x53D6;&#x5BF9;&#x8C61;<strong>&#x7C7B;&#x578B;</strong>,&#x914D;&#x5408;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x53EF;&#x5927;&#x81F4;&#x731C;&#x6D4B;&#x51FA;&#x6709;&#x54EA;&#x4E9B;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;&#x53EF;&#x4F9B;&#x5916;&#x90E8;&#x8C03;&#x7528;.
<code>print dir(response)</code> : &#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;<strong>&#x679A;&#x4E3E;&#x503C;</strong>,&#x65E0;&#x6587;&#x6863;&#x731C;&#x6D4B;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;.</p>
</blockquote>
<p>&#x901A;&#x8FC7; <code>urllib2.urlopen(url)</code> &#x5DF2;&#x7ECF;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x6700;&#x7B80;&#x5355;&#x7684;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x4E86;, &#x65E0;&#x8BBA;&#x662F; <code>GET</code> &#x8BF7;&#x6C42;&#x8FD8;&#x662F; <code>POST</code> &#x8BF7;&#x6C42;,&#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x540E;&#x7684;&#x54CD;&#x5E94;&#x4F53;&#x65E0;&#x7591;&#x662F;&#x975E;&#x5E38;&#x91CD;&#x8981;&#x7684;,&#x4F46;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x540C;&#x6837;&#x4E0D;&#x53EF;&#x5FFD;&#x7565;&#x7684;&#x662F;&#x8FD8;&#x6709;&#x5176;&#x4ED6;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;.</p>
<p>&#x56E0;&#x6B64;,&#x9664;&#x4E86;&#x638C;&#x63E1; <code>response.read()</code> &#x4E00;&#x6B21;&#x6027;&#x5168;&#x90E8;&#x8BFB;&#x53D6;&#x54CD;&#x5E94;&#x4F53;&#x5185;&#x5BB9;&#x4E4B;&#x5916;,&#x8FD8;&#x9700;&#x8981;&#x77E5;&#x9053; <code>response</code> &#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;.</p>
<p>&#x901A;&#x8FC7; <code>type(response)</code> &#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x7C7B;&#x578B;&#x518D;&#x914D;&#x5408; <code>dir(response)</code> &#x83B7;&#x53D6;&#x5C5E;&#x6027;&#x679A;&#x4E3E;&#x503C;&#x5373;&#x53EF;<strong>&#x65E0;&#x6587;&#x6863;&#x5927;&#x81F4;&#x731C;&#x6D4B;</strong>&#x5BF9;&#x8C61;&#x6709;&#x54EA;&#x4E9B;&#x53EF;&#x4F9B;&#x8C03;&#x7528;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> type(response)
    <span class="hljs-keyword">print</span> dir(response)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F; <code>print type(response)</code> &#x548C; <code>print dir(response)</code> &#x7684;&#x8F93;&#x51FA;&#x5185;&#x5BB9;,&#x63A5;&#x4E0B;&#x6765;&#x5C06;&#x6311;&#x9009;&#x51FA;&#x5E38;&#x7528;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x6162;&#x6162;&#x8BB2;&#x89E3;.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># print type(response)</span>
&lt;<span class="hljs-built_in">type</span> <span class="hljs-string">&apos;instance&apos;</span>&gt;

<span class="hljs-comment"># print dir(response)</span>
[<span class="hljs-string">&apos;__doc__&apos;</span>, <span class="hljs-string">&apos;__init__&apos;</span>, <span class="hljs-string">&apos;__iter__&apos;</span>, <span class="hljs-string">&apos;__module__&apos;</span>, <span class="hljs-string">&apos;__repr__&apos;</span>, <span class="hljs-string">&apos;close&apos;</span>, <span class="hljs-string">&apos;code&apos;</span>, <span class="hljs-string">&apos;fileno&apos;</span>, <span class="hljs-string">&apos;fp&apos;</span>, <span class="hljs-string">&apos;getcode&apos;</span>, <span class="hljs-string">&apos;geturl&apos;</span>, <span class="hljs-string">&apos;headers&apos;</span>, <span class="hljs-string">&apos;info&apos;</span>, <span class="hljs-string">&apos;msg&apos;</span>, <span class="hljs-string">&apos;next&apos;</span>, <span class="hljs-string">&apos;read&apos;</span>, <span class="hljs-string">&apos;readline&apos;</span>, <span class="hljs-string">&apos;readlines&apos;</span>, <span class="hljs-string">&apos;url&apos;</span>]
</code></pre>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x7801;(&#x5C5E;&#x6027;)</li>
</ul>
<blockquote>
<p><code>response.code</code> : &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x7801;,&#x6B63;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#x662F; <code>200</code> &#x8868;&#x793A;&#x8BF7;&#x6C42;&#x6210;&#x529F;,&#x800C; <code>500</code> &#x662F;&#x5178;&#x578B;&#x7684;&#x7CFB;&#x7EDF;&#x9519;&#x8BEF;.</p>
</blockquote>
<p>&#x901A;&#x8FC7; <code>dir(response)</code> &#x83B7;&#x53D6;&#x5C5E;&#x6027;&#x679A;&#x4E3E;&#x503C;,&#x7ED3;&#x5408; <code>type(response)</code> &#x4E0D;&#x96BE;&#x53D1;&#x73B0; <code>response.code</code> &#x662F;&#x7528;&#x6765;&#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x72B6;&#x6001;&#x7801;&#x7684;,&#x5177;&#x4F53;&#x8C03;&#x7528;&#x65B9;&#x5F0F;&#x662F; <code>response.code</code> &#x8FD8;&#x662F; <code>response.code()</code> &#x53EF;&#x8FD0;&#x884C; <code>print type(response.code)</code> &#x5927;&#x81F4;&#x63A8;&#x65AD;.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> type(response.read)
    <span class="hljs-keyword">print</span> type(response.code)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<blockquote>
<p>&#x8FD9;&#x91CC;&#x4E0D;&#x59A8;&#x7ED3;&#x5408; <code>print type(response.read)</code> &#x662F;&#x65B9;&#x6CD5;&#x6765;&#x9A8C;&#x8BC1;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x5230;&#x5E95;&#x662F;&#x5C5E;&#x6027;&#x8FD8;&#x662F;&#x65B9;&#x6CD5;,&#x53EF;&#x4EE5;&#x770B;&#x51FA; <code>response.read</code> &#x662F; <code>&lt;type &apos;instancemethod&apos;&gt;</code> &#x65B9;&#x6CD5;&#x7C7B;&#x578B;,&#x800C; <code>response.code</code> &#x662F; <code>&lt;type &apos;int&apos;&gt;</code> &#x57FA;&#x672C;&#x7C7B;&#x578B;,&#x56E0;&#x6B64; <code>response.code</code> &#x662F;&#x5C5E;&#x6027;&#x8C03;&#x7528;&#x65B9;&#x5F0F;.</p>
</blockquote>
<p><code>type(response.code)</code> &#x7684;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x662F; <code>&lt;type &apos;int&apos;&gt;</code> &#x5E76;&#x4E0D;&#x662F; <code>&lt;type &apos;instancemethod&apos;&gt;</code>,&#x56E0;&#x6B64;&#x83B7;&#x53D6;&#x72B6;&#x6001;&#x7801;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x5C5E;&#x6027;&#x8C03;&#x7528;.</p>
<p>&#x8BE6;&#x7EC6;&#x4EE3;&#x7801;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> response.code

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x7801;(&#x65B9;&#x6CD5;)</li>
</ul>
<blockquote>
<p><code>response.getcode()</code> : &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x7801;,&#x6B63;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#x662F; <code>200</code> &#x8868;&#x793A;&#x8BF7;&#x6C42;&#x6210;&#x529F;,&#x800C; <code>500</code> &#x662F;&#x5178;&#x578B;&#x7684;&#x7CFB;&#x7EDF;&#x9519;&#x8BEF;.</p>
</blockquote>
<p>&#x540C;&#x6837;&#x5730;,&#x4ECE; <code>print dir(response)</code> &#x53EF;&#x77E5; <code>getcode</code> &#x5B57;&#x6BB5;&#x53EF;&#x4F9B;&#x8C03;&#x7528;,&#x4F46;&#x4E0D;&#x77E5;&#x9053;&#x662F;&#x5C5E;&#x6027;&#x8C03;&#x7528;&#x8FD8;&#x662F;&#x65B9;&#x6CD5;&#x8C03;&#x7528;?&#x518D;&#x6B21;&#x4F7F;&#x7528; <code>print type(response.getcode)</code> &#x5F97;&#x5230; <code>&lt;type &apos;instancemethod&apos;&gt;</code> &#x56E0;&#x800C;&#x5224;&#x5B9A;&#x4E3A;&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x5F62;&#x5F0F;.</p>
<p>&#x8BE6;&#x60C5;&#x4EE3;&#x7801;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> response.getcode()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x7801;&#x4FE1;&#x606F;(&#x5C5E;&#x6027;)</li>
</ul>
<blockquote>
<p><code>response.msg</code> : &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;,&#x4F8B;&#x5982;&#x72B6;&#x6001;&#x7801; <code>200</code> &#x5BF9;&#x4E8E; <code>OK</code>,&#x800C; <code>500</code> &#x5BF9;&#x4E8E; <code>INTERNAL SERVER ERROR</code>.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x72B6;&#x6001;&#x7801;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/status/200&apos;</span>)
    <span class="hljs-keyword">print</span> response.code
    <span class="hljs-keyword">print</span> response.msg

    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/status/500&apos;</span>)
    <span class="hljs-keyword">print</span> response.code
    <span class="hljs-keyword">print</span> response.msg

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<blockquote>
<p>&#x6B63;&#x5E38;&#x8BF7;&#x6C42;&#x72B6;&#x6001;&#x662F; <code>200 OK</code>,&#x800C;&#x8BF7;&#x6C42;&#x53D1;&#x751F;&#x5F02;&#x5E38;&#x5F88;&#x53EF;&#x80FD;&#x662F; <code>500 INTERNAL SERVER ERROR</code> ,&#x4E00;&#x65E6;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x5982;&#x82E5;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x5219;&#x4F1A;&#x62A5;&#x9519;,&#x7A0B;&#x5E8F;&#x7EC8;&#x6B62;&#x8FD0;&#x884C;.</p>
</blockquote>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x8BBF;&#x95EE;&#x94FE;&#x63A5;(&#x5C5E;&#x6027;)</li>
</ul>
<blockquote>
<p><code>response.url</code> : &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x94FE;&#x63A5;.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> response.url

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x8BBF;&#x95EE;&#x94FE;&#x63A5;(&#x65B9;&#x6CD5;)</li>
</ul>
<blockquote>
<p><code>response.geturl()</code> : &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x94FE;&#x63A5;.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> response.geturl()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x8BBF;&#x95EE;&#x94FE;&#x63A5;(&#x5C5E;&#x6027;)</li>
</ul>
<blockquote>
<p><code>response.headers.dict</code> : &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x5934;&#x4FE1;&#x606F;&#x5E76;&#x4EE5;&#x5B57;&#x5178;&#x5F62;&#x5F0F;&#x663E;&#x793A;.</p>
</blockquote>
<p>&#x5728;&#x67D0;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x65F6;&#x5FC5;&#x987B;&#x643A;&#x5E26;&#x7279;&#x5B9A;&#x7684;&#x8BF7;&#x6C42;&#x5934;&#x65B9;&#x53EF;&#x6210;&#x529F;,&#x56E0;&#x6B64;&#x9700;&#x8981;&#x6E05;&#x695A;&#x9ED8;&#x8BA4;&#x4E0D;&#x8BBE;&#x7F6E;&#x8BF7;&#x6C42;&#x5934;&#x65F6;&#x670D;&#x52A1;&#x7AEF;&#x63A5;&#x6536;&#x5230;&#x7684;&#x8BF7;&#x6C42;&#x5934;&#x662F;&#x4EC0;&#x4E48;&#x6837;&#x5B50;&#x7684;,&#x540C;&#x6837;&#x5730;,&#x53EF;&#x4F7F;&#x7528; <code>print type(response.headers)</code> &#x7ED3;&#x5408; <code>print dir(response.headers)</code> &#x81EA;&#x884C;&#x63A2;&#x7D22;&#x53EF;&#x4F9B;&#x8C03;&#x7528;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> response.headers.dict

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x8BF7;&#x6C42;&#x5934;&#x4FE1;&#x606F;(&#x65B9;&#x6CD5;)</li>
</ul>
<blockquote>
<p><code>response.info()</code> : &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x5934;&#x4FE1;&#x606F;&#x5E76;&#x4EE5;&#x9010;&#x884C;&#x663E;&#x793A;</p>
</blockquote>
<p>&#x548C;&#x4E0A;&#x4E00;&#x4E2A; <code>response.headers.dict</code> &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x5934;&#x4FE1;&#x606F;&#x7C7B;&#x4F3C;,&#x53EA;&#x4E0D;&#x8FC7; <code>response.info()</code> &#x9002;&#x5408;&#x8089;&#x773C;&#x663E;&#x793A;&#x800C;&#x975E;&#x7A0B;&#x5E8F;&#x4F7F;&#x7528;.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x65B9;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> response.info()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x54CD;&#x5E94;&#x4F53;(&#x65B9;&#x6CD5;)</li>
</ul>
<blockquote>
<p><code>response.read()</code> : &#x4E00;&#x6B21;&#x6027;&#x8BFB;&#x53D6;&#x54CD;&#x5E94;&#x4F53;,&#x9002;&#x5408;&#x54CD;&#x5E94;&#x4F53;&#x6570;&#x636E;&#x91CF;&#x6BD4;&#x8F83;&#x5C0F;&#x7684;&#x60C5;&#x51B5;,&#x4E00;&#x6B21;&#x6027;&#x5168;&#x90E8;&#x8BFB;&#x53D6;&#x5230;&#x5185;&#x5B58;&#x65B9;&#x4FBF;&#x64CD;&#x4F5C;.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">print</span> response.read()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<p><code>response.read()</code> &#x8FD4;&#x56DE;&#x7684;&#x662F;&#x5B57;&#x7B26;&#x4E32;,&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x5F88;&#x65B9;&#x4FBF;&#x7528;&#x53D8;&#x91CF;&#x63A5;&#x6536;&#x4F5C;&#x540E;&#x7EED;&#x5904;&#x7406;,&#x4F8B;&#x5982; <code>result = response.read()</code> :</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    result = response.read()
    <span class="hljs-keyword">print</span> result

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x54CD;&#x5E94;&#x4F53;(&#x65B9;&#x6CD5;)</li>
</ul>
<blockquote>
<p><code>response.readline()</code> : &#x9010;&#x884C;&#x8BFB;&#x53D6;&#x54CD;&#x5E94;&#x4F53;,&#x9002;&#x7528;&#x4E8E;&#x6570;&#x636E;&#x4F53;&#x6BD4;&#x8F83;&#x5927;&#x7684;&#x60C5;&#x51B5;,&#x5FAA;&#x73AF;&#x8BFB;&#x53D6;&#x76F4;&#x5230;&#x6700;&#x7EC8;&#x65E0;&#x6570;&#x636E;&#x53EF;&#x8BFB;&#x53D6;&#x4E3A;&#x6B62;.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    line = response.readline()
    <span class="hljs-keyword">while</span> line:
        <span class="hljs-keyword">print</span> line
        line = response.readline()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<p><code>response.readline()</code> &#x53EA;&#x80FD;&#x9010;&#x884C;&#x8BFB;&#x53D6;,&#x56E0;&#x6B64;&#x60F3;&#x8981;&#x83B7;&#x53D6;&#x5B8C;&#x6210;&#x7684;&#x54CD;&#x5E94;&#x4F53;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x624B;&#x52A8;&#x62FC;&#x63A5;,&#x4F8B;&#x5982;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    result = <span class="hljs-string">&apos;&apos;</span>
    line = response.readline()
    result = result + str(line)
    <span class="hljs-keyword">while</span> line:
        line = response.readline()
        result = result + str(line)
    <span class="hljs-keyword">print</span> result

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<blockquote>
<p><code>str(line)</code> &#x662F;&#x4E3A;&#x4E86;&#x4FDD;&#x8BC1;&#x54CD;&#x5E94;&#x4F53;&#x5B57;&#x7B26;&#x4E32;&#x4E00;&#x5B9A;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;,&#x5176;&#x5B9E;&#x5E94;&#x8BE5;&#x4E0D;&#x5FC5;&#x5982;&#x6B64;,<code>response.readline()</code> &#x672C;&#x8EAB;&#x5DF2;&#x7ECF;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;&#x4E86;.</p>
</blockquote>
<ul>
<li>&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;&#x54CD;&#x5E94;&#x4F53;(&#x65B9;&#x6CD5;)</li>
</ul>
<blockquote>
<p><code>response.readlines()</code> : &#x904D;&#x5386;&#x8BFB;&#x53D6;&#x54CD;&#x5E94;&#x4F53;,&#x5FAA;&#x73AF;&#x8BFB;&#x53D6;&#x4E14;&#x4FDD;&#x5B58;&#x5230;&#x5217;&#x8868;&#x5BF9;&#x8C61;&#x4E2D;,&#x9002;&#x5408;&#x9700;&#x8981;&#x9010;&#x884C;&#x5904;&#x7406;&#x60C5;&#x51B5;.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> response.readlines():
        <span class="hljs-keyword">print</span> line

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<p>&#x540C;&#x6837;&#x5730;,&#x5982;&#x679C;&#x9700;&#x8981;&#x9488;&#x5BF9; <code>response.readlines()</code> &#x65B9;&#x5F0F;&#x83B7;&#x53D6;&#x5B8C;&#x6574;&#x54CD;&#x5E94;&#x4F53;&#x7ED3;&#x679C;,&#x53EF;&#x4EE5;&#x5982;&#x4E0B;&#x8FDB;&#x884C;&#x62FC;&#x63A5;,&#x793A;&#x4F8B;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    result = <span class="hljs-string">&apos;&apos;</span>
    <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> response.readlines():
        result = result + str(line)
    <span class="hljs-keyword">print</span> result

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2:&apos;</span>
    use_simple_urllib2()
</code></pre>
<blockquote>
<p>&#x4E0A;&#x8FF0;&#x591A;&#x884C;&#x4EE3;&#x7801;&#x8FD8;&#x53EF;&#x4EE5;&#x8FDB;&#x4E00;&#x6B65;&#x8F6C;&#x6362;&#x6210;&#x4E00;&#x884C;&#x4EE3;&#x7801;: <code>result = &apos;&apos;.join([line for line in response.readlines()])</code></p>
</blockquote>
<h3 id="&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A;-get-&#x8BF7;&#x6C42;"><a name="&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A;-get-&#x8BF7;&#x6C42;" class="anchor-navigation-ex-anchor" href="#&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A;-get-&#x8BF7;&#x6C42;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.3. &#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A; GET &#x8BF7;&#x6C42;</h3>
<ul>
<li>&#x65E0;&#x53C2;&#x6570;&#x76F4;&#x63A5;&#x53D1;&#x9001;</li>
</ul>
<blockquote>
<p><a href="urllib2.urlopen(url">urllib2.urlopen(url)</a>) : &#x53EA;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x76EE;&#x6807; <code>URL</code> &#x5373;&#x53EF;&#x53D1;&#x9001; <code>GET</code> &#x8BF7;&#x6C42;.</p>
</blockquote>
<p>&#x6700;&#x7B80;&#x5355;&#x7684;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x4E5F;&#x5C31;&#x662F; <code>GET</code> &#x65B9;&#x5F0F;,&#x4E0D;&#x8BBE;&#x7F6E;&#x5176;&#x4ED6;&#x53C2;&#x6570;&#x7684;&#x60C5;&#x51B5;&#x4E0B;,&#x53EA;&#x9700;&#x8981;&#x586B;&#x5199; <code>URL</code> &#x5373;&#x53EF;&#x53D1;&#x9001;&#x8BF7;&#x6C42;,&#x4F8B;&#x5982; <code>urllib2.urlopen(&apos;http://httpbin.snowdreams1006.cn/get&apos;)</code>,&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5934;&#x548C;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2&lt;&lt;&lt;&apos;</span>
    use_simple_urllib2()
</code></pre>
<p>&#x5047;&#x5982;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x6587;&#x4EF6;&#x540D;&#x4E3A; <code>urllib_demo.py</code>,&#x5728;&#x7EC8;&#x7AEF;&#x547D;&#x4EE4;&#x884C;&#x5185;&#x8FD0;&#x884C; <code>python urllib_demo.py</code> &#x6587;&#x4EF6;,&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#x6240;&#x793A;:</p>
<pre><code class="lang-bash">(.env) $ python urllib_demo.py 
&gt;&gt;&gt;Use simple urllib2&lt;&lt;&lt;
&gt;&gt;&gt;Response Headers:
Server: nginx/1.17.6
Date: Thu, 16 Jan 2020 13:38:27 GMT
Content-Type: application/json
Content-Length: 263
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: <span class="hljs-literal">true</span>

&gt;&gt;&gt;Response Body:
{
  <span class="hljs-string">&quot;args&quot;</span>: {}, 
  <span class="hljs-string">&quot;headers&quot;</span>: {
    <span class="hljs-string">&quot;Accept-Encoding&quot;</span>: <span class="hljs-string">&quot;identity&quot;</span>, 
    <span class="hljs-string">&quot;Connection&quot;</span>: <span class="hljs-string">&quot;close&quot;</span>, 
    <span class="hljs-string">&quot;Host&quot;</span>: <span class="hljs-string">&quot;httpbin.snowdreams1006.cn&quot;</span>, 
    <span class="hljs-string">&quot;User-Agent&quot;</span>: <span class="hljs-string">&quot;Python-urllib/2.7&quot;</span>
  }, 
  <span class="hljs-string">&quot;origin&quot;</span>: <span class="hljs-string">&quot;218.205.55.192&quot;</span>, 
  <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;http://httpbin.snowdreams1006.cn/get&quot;</span>
}
</code></pre>
<p>&#x5176;&#x4E2D;&#x54CD;&#x5E94;&#x5934; <code>Connection: close</code> &#x8868;&#x660E;&#x8FDE;&#x63A5;&#x662F;&#x81EA;&#x52A8;&#x5173;&#x95ED;&#x7684;,&#x800C;&#x54CD;&#x5E94;&#x4F53; <code>args</code> &#x662F;&#x7A7A;&#x5B57;&#x5178;&#x8868;&#x660E;&#x6CA1;&#x6709;&#x67E5;&#x8BE2;&#x53C2;&#x6570;.</p>
<ul>
<li>&#x6709;&#x53C2;&#x6570;&#x8F6C;&#x7801;&#x53D1;&#x9001;</li>
</ul>
<p>&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;,&#x5F88;&#x5C11;&#x6709; <code>GET</code> &#x8BF7;&#x6C42;&#x4E0D;&#x9700;&#x8981;&#x643A;&#x5E26;&#x53C2;&#x6570;&#x7684;,&#x5BF9;&#x4E8E;&#x6709;&#x53C2;&#x6570;&#x67E5;&#x8BE2;&#x7684; <code>GET</code> &#x8BF7;&#x6C42;,&#x539F;&#x751F; <code>urllib</code> &#x4E5F;&#x662F;&#x652F;&#x6301;&#x7684;,&#x6700;&#x7B80;&#x5355;&#x7684;&#x505A;&#x6CD5;&#x662F;&#x5C06;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x62FC;&#x63A5;&#x5230;&#x76EE;&#x6807; <code>URL</code> &#x4E0A;&#x5F97;&#x5230;&#x5E26;&#x6709;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x7684; <code>URL</code>.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_params_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5934;&#x548C;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get?param1=hello&amp;param2=world&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use params urllib2&lt;&lt;&lt;&apos;</span>
    use_params_urllib2()
</code></pre>
<p>&#x540C;&#x6837;&#x5730;,&#x5047;&#x5982;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x6587;&#x4EF6;&#x540D;&#x4E3A; <code>urllib_demo.py</code>,&#x5728;&#x7EC8;&#x7AEF;&#x547D;&#x4EE4;&#x884C;&#x5185;&#x8FD0;&#x884C; <code>python urllib_demo.py</code> &#x6587;&#x4EF6;,&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#x6240;&#x793A;:</p>
<pre><code class="lang-bash">(.env) $ python urllib_demo.py 
&gt;&gt;&gt;Use params urllib2&lt;&lt;&lt;
&gt;&gt;&gt;Response Headers:
Server: nginx/1.17.6
Date: Thu, 16 Jan 2020 13:59:23 GMT
Content-Type: application/json
Content-Length: 338
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: <span class="hljs-literal">true</span>

&gt;&gt;&gt;Response Body:
{
  <span class="hljs-string">&quot;args&quot;</span>: {
    <span class="hljs-string">&quot;param1&quot;</span>: <span class="hljs-string">&quot;hello&quot;</span>, 
    <span class="hljs-string">&quot;param2&quot;</span>: <span class="hljs-string">&quot;world&quot;</span>
  }, 
  <span class="hljs-string">&quot;headers&quot;</span>: {
    <span class="hljs-string">&quot;Accept-Encoding&quot;</span>: <span class="hljs-string">&quot;identity&quot;</span>, 
    <span class="hljs-string">&quot;Connection&quot;</span>: <span class="hljs-string">&quot;close&quot;</span>, 
    <span class="hljs-string">&quot;Host&quot;</span>: <span class="hljs-string">&quot;httpbin.snowdreams1006.cn&quot;</span>, 
    <span class="hljs-string">&quot;User-Agent&quot;</span>: <span class="hljs-string">&quot;Python-urllib/2.7&quot;</span>
  }, 
  <span class="hljs-string">&quot;origin&quot;</span>: <span class="hljs-string">&quot;218.205.55.192&quot;</span>, 
  <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;http://httpbin.snowdreams1006.cn/get?param1=hello&amp;param2=world&quot;</span>
}
</code></pre>
<p>&#x5176;&#x4E2D;&#x54CD;&#x5E94;&#x5934; <code>Connection: close</code> &#x8868;&#x660E;&#x8FDE;&#x63A5;&#x662F;&#x81EA;&#x52A8;&#x5173;&#x95ED;&#x7684;,&#x800C;&#x54CD;&#x5E94;&#x4F53; <code>args</code> &#x4E0D;&#x518D;&#x662F;&#x7A7A;&#x5B57;&#x5178;&#x800C;&#x662F;&#x521A;&#x624D;&#x4F20;&#x9012;&#x7684;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x8868;&#x660E;&#x670D;&#x52A1;&#x7AEF;&#x786E;&#x5B9E;&#x63A5;&#x6536;&#x5230;&#x53D1;&#x9001;&#x7684;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x4E86;,&#x6240;&#x4EE5;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x4E5F;&#x662F;&#x53EF;&#x884C;&#x7684;.</p>
<p>&#x5982;&#x679C;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x975E;&#x5E38;&#x591A;,&#x76F4;&#x63A5;&#x5728;&#x8BF7;&#x6C42;&#x94FE;&#x63A5; <code>URL</code> &#x57FA;&#x7840;&#x4E0A;&#x62FC;&#x63A5;&#x5F62;&#x6210;&#x65B0;&#x7684; <code>URL</code> &#x5C06;&#x4F1A;&#x663E;&#x793A;&#x975E;&#x5E38;&#x7E41;&#x7410;,&#x800C;&#x4E14;&#x5FC5;&#x987B;&#x9075;&#x5B88; <code>?param1=hello&amp;param2=world</code> &#x8FD9;&#x79CD;&#x683C;&#x5F0F;,&#x6240;&#x4EE5;&#x8FD9;&#x79CD;&#x7E41;&#x7410;&#x7684;&#x62FC;&#x63A5;&#x5DE5;&#x4F5C;&#x5C31;&#x4EA4;&#x7ED9;&#x7A0B;&#x5E8F;&#x53BB;&#x5B8C;&#x6210;&#x5427;!</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_params_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5934;&#x548C;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get?param1=hello&amp;param2=world&amp;author=snowdreams1006&amp;website=http://blog.snowdreams1006.cn&amp;url=snowdreams1006.github.io/learn-python/url/urllib/teaching.html&amp;wechat=snowdreams1006&amp;email=snowdreams1006@163.com&amp;github=https://github.com/snowdreams1006/&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use params urllib2&lt;&lt;&lt;&apos;</span>
    use_params_urllib2()
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x7E41;&#x7410;&#x4E0D;&#x4EC5;&#x4F53;&#x73B0;&#x5728;&#x62FC;&#x63A5;&#x6210;&#x65B0;&#x7684; <code>URL</code> &#x65F6;&#x957F;&#x5EA6;&#x8FC7;&#x957F;&#x5BB9;&#x5668;&#x51FA;&#x9519;,&#x8FD8;&#x4F1A;&#x9047;&#x5230;&#x52A8;&#x6001;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x66FF;&#x6362;&#x7684;&#x95EE;&#x9898;,&#x6240;&#x4EE5;&#x81EA;&#x52A8;&#x62FC;&#x63A5;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x529F;&#x80FD;&#x771F;&#x7684;&#x662F;&#x53CA;&#x65F6;&#x96E8;!</p>
<pre><code class="lang-python">params = urllib.urlencode({
    <span class="hljs-string">&apos;param1&apos;</span>: <span class="hljs-string">&apos;hello&apos;</span>, 
    <span class="hljs-string">&apos;param2&apos;</span>: <span class="hljs-string">&apos;world&apos;</span>,
    <span class="hljs-string">&apos;author&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
    <span class="hljs-string">&apos;website&apos;</span>:<span class="hljs-string">&apos;http://blog.snowdreams1006.cn&apos;</span>,
    <span class="hljs-string">&apos;url&apos;</span>:<span class="hljs-string">&apos;https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html&apos;</span>,
    <span class="hljs-string">&apos;wechat&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
    <span class="hljs-string">&apos;email&apos;</span>:<span class="hljs-string">&apos;snowdreams1006@163.com&apos;</span>,
    <span class="hljs-string">&apos;github&apos;</span>:<span class="hljs-string">&apos;https://github.com/snowdreams1006/&apos;</span>
})
<span class="hljs-keyword">print</span> params
</code></pre>
<p><code>urllib.urlencode()</code> &#x53EF;&#x4EE5;&#x5C06;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#x7684;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x8F6C;&#x7801;&#x62FC;&#x63A5;&#x6210; <code>&amp;</code> &#x8FDE;&#x63A5;&#x7684;&#x67E5;&#x8BE2;&#x53C2;&#x6570;,&#x4E4B;&#x540E;&#x518D;&#x624B;&#x52A8;&#x62FC;&#x63A5;&#x5230;&#x8BF7;&#x6C42; <code>URL?params</code> &#x5373;&#x53EF;&#x5F97;&#x5230;&#x5E26;&#x53C2;&#x6570;&#x7684; <code>URL</code>.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_params_urllib2</span><span class="hljs-params">()</span>:</span>
    params = urllib.urlencode({
        <span class="hljs-string">&apos;param1&apos;</span>: <span class="hljs-string">&apos;hello&apos;</span>, 
        <span class="hljs-string">&apos;param2&apos;</span>: <span class="hljs-string">&apos;world&apos;</span>,
        <span class="hljs-string">&apos;author&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;website&apos;</span>:<span class="hljs-string">&apos;http://blog.snowdreams1006.cn&apos;</span>,
        <span class="hljs-string">&apos;url&apos;</span>:<span class="hljs-string">&apos;https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html&apos;</span>,
        <span class="hljs-string">&apos;wechat&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;email&apos;</span>:<span class="hljs-string">&apos;snowdreams1006@163.com&apos;</span>,
        <span class="hljs-string">&apos;github&apos;</span>:<span class="hljs-string">&apos;https://github.com/snowdreams1006/&apos;</span>
    })
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get?%s&apos;</span> % params)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use params urllib2&lt;&lt;&lt;&apos;</span>
    use_params_urllib2()
</code></pre>
<p>&#x5047;&#x5982;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x6587;&#x4EF6;&#x540D;&#x4E3A; <code>urllib_demo.py</code>,&#x5728;&#x7EC8;&#x7AEF;&#x547D;&#x4EE4;&#x884C;&#x5185;&#x8FD0;&#x884C; <code>python urllib_demo.py</code> &#x6587;&#x4EF6;,&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#x6240;&#x793A;:</p>
<pre><code class="lang-bash">$ python urllib_demo.py 
&gt;&gt;&gt;Use params urllib2&lt;&lt;&lt;
&gt;&gt;&gt;Response Headers:
Server: nginx/1.17.6
Date: Thu, 16 Jan 2020 14:27:21 GMT
Content-Type: application/json
Content-Length: 892
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: <span class="hljs-literal">true</span>

&gt;&gt;&gt;Response Body:
{
  <span class="hljs-string">&quot;args&quot;</span>: {
    <span class="hljs-string">&quot;author&quot;</span>: <span class="hljs-string">&quot;snowdreams1006&quot;</span>, 
    <span class="hljs-string">&quot;email&quot;</span>: <span class="hljs-string">&quot;snowdreams1006@163.com&quot;</span>, 
    <span class="hljs-string">&quot;github&quot;</span>: <span class="hljs-string">&quot;https://github.com/snowdreams1006/&quot;</span>, 
    <span class="hljs-string">&quot;param1&quot;</span>: <span class="hljs-string">&quot;hello&quot;</span>, 
    <span class="hljs-string">&quot;param2&quot;</span>: <span class="hljs-string">&quot;world&quot;</span>, 
    <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html&quot;</span>,
    <span class="hljs-string">&quot;website&quot;</span>: <span class="hljs-string">&quot;http://blog.snowdreams1006.cn&quot;</span>, 
    <span class="hljs-string">&quot;wechat&quot;</span>: <span class="hljs-string">&quot;snowdreams1006&quot;</span>
  }, 
  <span class="hljs-string">&quot;headers&quot;</span>: {
    <span class="hljs-string">&quot;Accept-Encoding&quot;</span>: <span class="hljs-string">&quot;identity&quot;</span>, 
    <span class="hljs-string">&quot;Connection&quot;</span>: <span class="hljs-string">&quot;close&quot;</span>, 
    <span class="hljs-string">&quot;Host&quot;</span>: <span class="hljs-string">&quot;httpbin.snowdreams1006.cn&quot;</span>, 
    <span class="hljs-string">&quot;User-Agent&quot;</span>: <span class="hljs-string">&quot;Python-urllib/2.7&quot;</span>
  }, 
  <span class="hljs-string">&quot;origin&quot;</span>: <span class="hljs-string">&quot;218.205.55.192&quot;</span>, 
  <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;http://httpbin.snowdreams1006.cn/get?website=http%3A%2F%2Fblog.snowdreams1006.cn&amp;github=https%3A%2F%2Fgithub.com%2Fsnowdreams1006%2F&amp;wechat=snowdreams1006&amp;param2=world&amp;param1=hello&amp;author=snowdreams1006&amp;url=https%3A%2F%2Fsnowdreams1006.github.io%2Flearn-python%2Furl%2Furllib%2Fteaching.html&amp;email=snowdreams1006%40163.com&quot;</span>
}
</code></pre>
<p>&#x7531;&#x6B64;&#x53EF;&#x89C1;,&#x4E0D;&#x8BBA;&#x662F;&#x76F4;&#x63A5;&#x624B;&#x52A8;&#x62FC;&#x63A5;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x8FD8;&#x662F;&#x4F7F;&#x7528; <code>urllib.urlencode(query)</code> &#x534A;&#x624B;&#x52A8;&#x62FC;&#x63A5;&#x67E5;&#x8BE2;&#x53C2;&#x6570;,&#x672C;&#x8D28;&#x4E0A;&#x90FD;&#x662F;&#x4E00;&#x6837;&#x7684;,&#x4F9D;&#x7136;&#x662F;&#x4F7F;&#x7528; <code>urllib2.urlopen(url)</code> &#x53D1;&#x9001; <code>GET</code> &#x8BF7;&#x6C42;.</p>
<h3 id="&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A;-post-&#x8BF7;&#x6C42;"><a name="&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A;-post-&#x8BF7;&#x6C42;" class="anchor-navigation-ex-anchor" href="#&#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A;-post-&#x8BF7;&#x6C42;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.4. &#x5982;&#x4F55;&#x53D1;&#x9001;&#x666E;&#x901A; POST &#x8BF7;&#x6C42;</h3>
<p>&#x5982;&#x679C;&#x8BF7;&#x6C42;&#x94FE;&#x63A5; <code>URL</code> &#x4EC5;&#x4EC5;&#x652F;&#x6301; <code>POST</code> &#x8BF7;&#x6C42;,&#x8FD9;&#x65F6;&#x4E0A;&#x8FF0;&#x62FC;&#x63A5;&#x5730;&#x5740;&#x5B9E;&#x73B0;&#x7684; <code>GET</code> &#x8BF7;&#x6C42;&#x5C31;&#x4E0D;&#x518D;&#x6EE1;&#x8DB3;&#x8981;&#x6C42;,&#x6709;&#x610F;&#x601D;&#x7684;&#x662F;,&#x7ADF;&#x7136;&#x53EA;&#x9700;&#x8981;&#x4E00;&#x6B65;&#x5C31;&#x53EF;&#x4EE5;&#x5C06; <code>GET</code> &#x8BF7;&#x6C42;&#x8F6C;&#x6362;&#x6210; <code>POST</code> &#x8BF7;&#x6C42;.</p>
<p>&#x5982;&#x679C;&#x662F; <code>GET</code> &#x8BF7;&#x6C42;,&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x65F6;&#x662F;&#x8FD9;&#x6837;: <code>urllib2.urlopen(&apos;http://httpbin.snowdreams1006.cn/post?%s&apos; % params)</code>;</p>
<p>&#x5982;&#x679C;&#x662F; <code>POST</code> &#x8BF7;&#x6C42;,&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x65F6;&#x662F;&#x8FD9;&#x6837;: <code>urllib2.urlopen(&apos;http://httpbin.snowdreams1006.cn/post&apos;,params)</code>;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post_params_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5934;&#x548C;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    params = urllib.urlencode({
        <span class="hljs-string">&apos;param1&apos;</span>: <span class="hljs-string">&apos;hello&apos;</span>, 
        <span class="hljs-string">&apos;param2&apos;</span>: <span class="hljs-string">&apos;world&apos;</span>,
        <span class="hljs-string">&apos;author&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;website&apos;</span>:<span class="hljs-string">&apos;http://blog.snowdreams1006.cn&apos;</span>,
        <span class="hljs-string">&apos;url&apos;</span>:<span class="hljs-string">&apos;https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html&apos;</span>,
        <span class="hljs-string">&apos;wechat&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;email&apos;</span>:<span class="hljs-string">&apos;snowdreams1006@163.com&apos;</span>,
        <span class="hljs-string">&apos;github&apos;</span>:<span class="hljs-string">&apos;https://github.com/snowdreams1006/&apos;</span>
    })
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/post&apos;</span>,params)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Post params urllib2&lt;&lt;&lt;&apos;</span>
    post_params_urllib2()
</code></pre>
<p>&#x7531;&#x4E8E; <code>GET</code> &#x8BF7;&#x6C42;&#x548C; <code>POST</code> &#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x5B9E;&#x5728;&#x592A;&#x50CF;&#x4E86;,&#x56E0;&#x6B64;&#x9700;&#x8981;&#x7559;&#x610F;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x65F6; <code>urllib2.urlopen(url)</code> &#x4E2D;&#x94FE;&#x63A5; <code>URL</code> &#x5230;&#x5E95;&#x662F;&#x600E;&#x4E48;&#x62FC;&#x63A5;&#x7684;?</p>
<p>&#x4E0D;&#x8FC7;&#x66F4;&#x52A0;&#x76F4;&#x89C2;&#x7684;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x76F4;&#x63A5;&#x9A8C;&#x8BC1;,&#x793A;&#x4F8B;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-bash">(.env) $ python urllib_demo.py 
&gt;&gt;&gt;Post params urllib2&lt;&lt;&lt;
&gt;&gt;&gt;Response Headers:
Server: nginx/1.17.6
Date: Thu, 16 Jan 2020 14:45:43 GMT
Content-Type: application/json
Content-Length: 758
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: <span class="hljs-literal">true</span>

&gt;&gt;&gt;Response Body:
{
  <span class="hljs-string">&quot;args&quot;</span>: {}, 
  <span class="hljs-string">&quot;data&quot;</span>: <span class="hljs-string">&quot;&quot;</span>, 
  <span class="hljs-string">&quot;files&quot;</span>: {}, 
  <span class="hljs-string">&quot;form&quot;</span>: {
    <span class="hljs-string">&quot;author&quot;</span>: <span class="hljs-string">&quot;snowdreams1006&quot;</span>, 
    <span class="hljs-string">&quot;email&quot;</span>: <span class="hljs-string">&quot;snowdreams1006@163.com&quot;</span>, 
    <span class="hljs-string">&quot;github&quot;</span>: <span class="hljs-string">&quot;https://github.com/snowdreams1006/&quot;</span>, 
    <span class="hljs-string">&quot;param1&quot;</span>: <span class="hljs-string">&quot;hello&quot;</span>, 
    <span class="hljs-string">&quot;param2&quot;</span>: <span class="hljs-string">&quot;world&quot;</span>, 
    <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html&quot;</span>,
    <span class="hljs-string">&quot;website&quot;</span>: <span class="hljs-string">&quot;http://blog.snowdreams1006.cn&quot;</span>, 
    <span class="hljs-string">&quot;wechat&quot;</span>: <span class="hljs-string">&quot;snowdreams1006&quot;</span>
  }, 
  <span class="hljs-string">&quot;headers&quot;</span>: {
    <span class="hljs-string">&quot;Accept-Encoding&quot;</span>: <span class="hljs-string">&quot;identity&quot;</span>, 
    <span class="hljs-string">&quot;Connection&quot;</span>: <span class="hljs-string">&quot;close&quot;</span>, 
    <span class="hljs-string">&quot;Content-Length&quot;</span>: <span class="hljs-string">&quot;285&quot;</span>, 
    <span class="hljs-string">&quot;Content-Type&quot;</span>: <span class="hljs-string">&quot;application/x-www-form-urlencoded&quot;</span>, 
    <span class="hljs-string">&quot;Host&quot;</span>: <span class="hljs-string">&quot;httpbin.snowdreams1006.cn&quot;</span>, 
    <span class="hljs-string">&quot;User-Agent&quot;</span>: <span class="hljs-string">&quot;Python-urllib/2.7&quot;</span>
  }, 
  <span class="hljs-string">&quot;json&quot;</span>: null, 
  <span class="hljs-string">&quot;origin&quot;</span>: <span class="hljs-string">&quot;218.205.55.192&quot;</span>, 
  <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;http://httpbin.snowdreams1006.cn/post&quot;</span>
}
</code></pre>
<p>&#x503C;&#x5F97;&#x6CE8;&#x610F;&#x7684;&#x662F;,&#x4E0A;&#x8FF0; <code>POST</code> &#x8BF7;&#x6C42;&#x63D0;&#x4EA4;&#x7684;&#x53C2;&#x6570;&#x5B58;&#x653E;&#x5728; <code>form</code> &#x5C5E;&#x6027;&#x800C;&#x4E0D;&#x662F; <code>GET</code> &#x8BF7;&#x6C42;&#x65F6;&#x7684; <code>args</code> &#x5C5E;&#x6027;.</p>
<h3 id="&#x5982;&#x4F55;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;&#x8FDB;&#x884C;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;"><a name="&#x5982;&#x4F55;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;&#x8FDB;&#x884C;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;" class="anchor-navigation-ex-anchor" href="#&#x5982;&#x4F55;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;&#x8FDB;&#x884C;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.5. &#x5982;&#x4F55;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;&#x8FDB;&#x884C;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;</h3>
<h4 id="&#x73AF;&#x5883;&#x642D;&#x5EFA;_2"><a name="&#x73AF;&#x5883;&#x642D;&#x5EFA;_2" class="anchor-navigation-ex-anchor" href="#&#x73AF;&#x5883;&#x642D;&#x5EFA;_2"><i class="fa fa-link" aria-hidden="true"></i></a>&#x73AF;&#x5883;&#x642D;&#x5EFA;</h4>
<p>&#x5982;&#x679C; <a href="http://proxyip.snowdreams1006.cn/" target="_blank">http://proxyip.snowdreams1006.cn/</a> &#x65E0;&#x6CD5;&#x8BBF;&#x95EE;,&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;<a href="https://github.com/jhao104/proxy_pool" target="_blank">https://github.com/jhao104/proxy_pool</a>&#x9879;&#x76EE;&#x81EA;&#x884C;&#x6784;&#x5EFA;&#x4EE3;&#x7406;&#x6C60;.</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;delete?proxy=127.0.0.1:8080&quot;</span>: <span class="hljs-string">&quot;delete an unable proxy&quot;</span>, 
  <span class="hljs-string">&quot;get&quot;</span>: <span class="hljs-string">&quot;get an useful proxy&quot;</span>, 
  <span class="hljs-string">&quot;get_all&quot;</span>: <span class="hljs-string">&quot;get all proxy from proxy pool&quot;</span>, 
  <span class="hljs-string">&quot;get_status&quot;</span>: <span class="hljs-string">&quot;proxy number&quot;</span>
}
</code></pre>
<blockquote>
<p>&#x5355;&#x673A;&#x52FF;&#x538B;,&#x6076;&#x610F;&#x8BBF;&#x95EE;&#x4F1A;&#x5173;&#x5C0F;&#x9ED1;&#x5C4B;&#x54DF;,&#x63A8;&#x8350;&#x5927;&#x5BB6;&#x81EA;&#x884C;&#x642D;&#x5EFA;&#x672C;&#x5730;&#x73AF;&#x5883;,&#x8C22;&#x8C22;&#x652F;&#x6301;.</p>
</blockquote>
<p>&#x672C;&#x4EE3;&#x7406;&#x6C60;&#x662F;&#x57FA;&#x4E8E; <a href="https://github.com/jhao104/proxy_pool" target="_blank">jhao104/proxy_pool</a>&#x9879;&#x76EE;&#x63D0;&#x4F9B;&#x4E24;&#x79CD;&#x5B89;&#x88C5;&#x65B9;&#x5F0F;,&#x5206;&#x4E3A; <code>docker</code> &#x5B89;&#x88C5;&#x65B9;&#x5F0F;&#x548C;&#x6E90;&#x7801;&#x5B89;&#x88C5;&#x65B9;&#x5F0F;.</p>
<h5 id="docker-&#x65B9;&#x5F0F;&#x5B89;&#x88C5;"><a name="docker-&#x65B9;&#x5F0F;&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#docker-&#x65B9;&#x5F0F;&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a><code>docker</code> &#x65B9;&#x5F0F;&#x5B89;&#x88C5;</h5>
<pre><code class="lang-bash">docker run --env db_<span class="hljs-built_in">type</span>=REDIS --env db_host=127.0.0.1 --env db_port=6379 --env db_password=<span class="hljs-string">&apos;&apos;</span> -p 5010:5010 jhao104/proxy_pool
</code></pre>
<blockquote>
<p>&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x63D0;&#x524D;&#x4E0B;&#x8F7D;&#x955C;&#x50CF;: <code>docker pull jhao104/proxy_pool</code>,&#x7136;&#x540E;&#x518D;&#x8FD0;&#x884C;&#x4E0A;&#x8FF0;&#x547D;&#x4EE4;&#x542F;&#x52A8;&#x5BB9;&#x5668;.</p>
</blockquote>
<h5 id="&#x6E90;&#x7801;&#x65B9;&#x5F0F;&#x5B89;&#x88C5;"><a name="&#x6E90;&#x7801;&#x65B9;&#x5F0F;&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#&#x6E90;&#x7801;&#x65B9;&#x5F0F;&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6E90;&#x7801;&#x65B9;&#x5F0F;&#x5B89;&#x88C5;</h5>
<ul>
<li>&#x6B65;&#x9AA4; 1 : &#x4E0B;&#x8F7D;&#x6E90;&#x7801;</li>
</ul>
<pre><code class="lang-bash">git <span class="hljs-built_in">clone</span> https://github.com/jhao104/proxy_pool.git
</code></pre>
<blockquote>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4E0B;&#x8F7D;&#x5B89;&#x88C5;&#x5305;: <a href="https://github.com/jhao104/proxy_pool/releases" target="_blank">https://github.com/jhao104/proxy_pool/releases</a></p>
</blockquote>
<ul>
<li>&#x6B65;&#x9AA4; 2 : &#x5B89;&#x88C5;&#x4F9D;&#x8D56;</li>
</ul>
<pre><code class="lang-bash">pip install -r requirements.txt
</code></pre>
<blockquote>
<p>&#x5B89;&#x88C5;&#x9879;&#x76EE;&#x4F9D;&#x8D56;&#x65F6;&#x9700;&#x8981;&#x5207;&#x6362;&#x5230;&#x9879;&#x76EE;&#x6839;&#x76EE;&#x5F55;,&#x4F8B;&#x5982; <code>cd proxy_pool</code>,&#x7136;&#x540E;&#x4F1A;&#x81EA;&#x52A8;&#x4ECE;&#x9ED8;&#x8BA4;&#x5B89;&#x88C5;&#x6E90;&#x8FDB;&#x884C;&#x4E0B;&#x8F7D;,&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x4F7F;&#x7528; <code>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt</code> &#x52A0;&#x901F;&#x5B89;&#x88C5;.</p>
</blockquote>
<ul>
<li>&#x6B65;&#x9AA4; 3 : &#x914D;&#x7F6E; <code>Config/setting.py</code></li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># Config/setting.py &#x4E3A;&#x9879;&#x76EE;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span>

<span class="hljs-comment"># &#x914D;&#x7F6E;DB     </span>
DATABASES = {
    <span class="hljs-string">&quot;default&quot;</span>: {
        <span class="hljs-string">&quot;TYPE&quot;</span>: <span class="hljs-string">&quot;REDIS&quot;</span>,        <span class="hljs-comment"># &#x76EE;&#x524D;&#x652F;&#x6301;SSDB&#x6216;REDIS&#x6570;&#x636E;&#x5E93;</span>
        <span class="hljs-string">&quot;HOST&quot;</span>: <span class="hljs-string">&quot;127.0.0.1&quot;</span>,   <span class="hljs-comment"># db host</span>
        <span class="hljs-string">&quot;PORT&quot;</span>: <span class="hljs-number">6379</span>,          <span class="hljs-comment"># db port&#xFF0C;&#x4F8B;&#x5982;SSDB&#x901A;&#x5E38;&#x4F7F;&#x7528;8888&#xFF0C;REDIS&#x901A;&#x5E38;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;6379</span>
        <span class="hljs-string">&quot;NAME&quot;</span>: <span class="hljs-string">&quot;proxy&quot;</span>,       <span class="hljs-comment"># &#x9ED8;&#x8BA4;&#x914D;&#x7F6E;</span>
        <span class="hljs-string">&quot;PASSWORD&quot;</span>: <span class="hljs-string">&quot;&quot;</span>         <span class="hljs-comment"># db password</span>
    }
}

<span class="hljs-comment"># &#x914D;&#x7F6E; API&#x670D;&#x52A1;</span>
SERVER_API = {
    <span class="hljs-string">&quot;HOST&quot;</span>: <span class="hljs-string">&quot;0.0.0.0&quot;</span>,  <span class="hljs-comment"># &#x76D1;&#x542C;ip, 0.0.0.0 &#x76D1;&#x542C;&#x6240;&#x6709;IP</span>
    <span class="hljs-string">&quot;PORT&quot;</span>: <span class="hljs-number">5010</span>        <span class="hljs-comment"># &#x76D1;&#x542C;&#x7AEF;&#x53E3;</span>
}

<span class="hljs-comment"># &#x4E0A;&#x9762;&#x914D;&#x7F6E;&#x542F;&#x52A8;&#x540E;&#xFF0C;&#x4EE3;&#x7406;&#x6C60;&#x8BBF;&#x95EE;&#x5730;&#x5740;&#x4E3A; http://127.0.0.1:5010</span>
</code></pre>
<ul>
<li>&#x6B65;&#x9AA4; 5 : &#x542F;&#x52A8;&#x9879;&#x76EE;</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-comment"># &#x5982;&#x679C;&#x4F60;&#x7684;&#x4F9D;&#x8D56;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x5B8C;&#x6210;&#x5E76;&#x4E14;&#x5177;&#x5907;&#x8FD0;&#x884C;&#x6761;&#x4EF6;,&#x53EF;&#x4EE5;&#x5728;cli&#x76EE;&#x5F55;&#x4E0B;&#x901A;&#x8FC7;ProxyPool.py&#x542F;&#x52A8;.</span>
<span class="hljs-comment"># &#x7A0B;&#x5E8F;&#x5206;&#x4E3A;: schedule &#x8C03;&#x5EA6;&#x7A0B;&#x5E8F; &#x548C; webserver Api&#x670D;&#x52A1;</span>

<span class="hljs-comment"># &#x9996;&#x5148;&#x542F;&#x52A8;&#x8C03;&#x5EA6;&#x7A0B;&#x5E8F;</span>
python proxyPool.py schedule

<span class="hljs-comment"># &#x7136;&#x540E;&#x542F;&#x52A8;webApi&#x670D;&#x52A1;</span>
python proxyPool.py webserver
</code></pre>
<blockquote>
<p>&#x8BE5;&#x547D;&#x4EE4;&#x8981;&#x6C42;&#x5F53;&#x524D;&#x73AF;&#x5883;&#x5904;&#x4E8E; <code>cli</code> &#x76EE;&#x5F55;,&#x5982;&#x679C;&#x662F;&#x5176;&#x4ED6;&#x76EE;&#x5F55;&#x8BF7;&#x81EA;&#x884C;&#x8C03;&#x6574; <code>proxyPool.py</code> &#x7684;&#x8DEF;&#x5F84;(<code>cd cli</code> &#x5373;&#x53EF;&#x5207;&#x6362;&#x5230; <code>cli</code> &#x76EE;&#x5F55;)</p>
</blockquote>
<p>&#x5982;&#x679C;&#x4EE5;&#x4E0A;&#x6B65;&#x9AA4;&#x5747;&#x6B63;&#x5E38;,&#x9879;&#x76EE;&#x542F;&#x52A8;&#x4F1A;&#x540E;&#x81EA;&#x52A8;&#x6293;&#x53D6;&#x4E92;&#x8054;&#x7F51;&#x514D;&#x8D39;&#x4EE3;&#x7406; ip,&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8BBF;&#x95EE; <a href="http://127.0.0.1:5010" target="_blank">http://127.0.0.1:5010</a> &#x67E5;&#x770B;.</p>
<h4 id="&#x4EE3;&#x7406;&#x8BF7;&#x6C42;"><a name="&#x4EE3;&#x7406;&#x8BF7;&#x6C42;" class="anchor-navigation-ex-anchor" href="#&#x4EE3;&#x7406;&#x8BF7;&#x6C42;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4EE3;&#x7406;&#x8BF7;&#x6C42;</h4>
<p>&#x5EFA;&#x8BAE;&#x9996;&#x5148;&#x5229;&#x7528;&#x6D4F;&#x89C8;&#x5668;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;<a href="http://proxyip.snowdreams1006.cn/get/" target="_blank">http://proxyip.snowdreams1006.cn/get/</a>&#x67E5;&#x770B;&#x662F;&#x5426;&#x80FD;&#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406; ip,&#x7136;&#x540E;&#x518D;&#x5229;&#x7528; <code>python</code> &#x7A0B;&#x5E8F;&#x83B7;&#x53D6;,&#x4FDD;&#x8BC1;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x6B63;&#x786E;,&#x65B9;&#x4FBF;&#x540E;&#x7EED;&#x5F00;&#x53D1;&#x6D4B;&#x8BD5;.</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;proxy&quot;</span>: <span class="hljs-string">&quot;183.91.33.41:8086&quot;</span>, 
    <span class="hljs-string">&quot;fail_count&quot;</span>: <span class="hljs-number">0</span>, 
    <span class="hljs-string">&quot;region&quot;</span>: <span class="hljs-string">&quot;&quot;</span>, 
    <span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;&quot;</span>, 
    <span class="hljs-string">&quot;source&quot;</span>: <span class="hljs-string">&quot;freeProxy09&quot;</span>, 
    <span class="hljs-string">&quot;check_count&quot;</span>: <span class="hljs-number">59</span>, 
    <span class="hljs-string">&quot;last_status&quot;</span>: <span class="hljs-number">1</span>, 
    <span class="hljs-string">&quot;last_time&quot;</span>: <span class="hljs-string">&quot;2020-01-18 13:14:32&quot;</span>
}
</code></pre>
<blockquote>
<p>&#x4E0A;&#x8FF0;&#x662F;&#x8BF7;&#x6C42; <code>/get/</code> &#x83B7;&#x53D6;&#x7684;&#x968F;&#x673A; ip &#x793A;&#x4F8B;,&#x5B8C;&#x6574;&#x8BF7;&#x6C42;&#x5730;&#x5740;: <code>http://proxyip.snowdreams1006.cn/get/</code></p>
</blockquote>
<h5 id="&#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406;-ip"><a name="&#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406;-ip" class="anchor-navigation-ex-anchor" href="#&#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406;-ip"><i class="fa fa-link" aria-hidden="true"></i></a>&#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406; ip</h5>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib2
<span class="hljs-keyword">import</span> json

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_proxy</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://proxyip.snowdreams1006.cn/get/&apos;</span>)
    result = response.read()
    <span class="hljs-keyword">return</span> json.loads(result)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Get proxy urllib&lt;&lt;&lt;&apos;</span>
    get_proxy_urllib()
</code></pre>
<blockquote>
<p>&#x5982;&#x679C;&#x6709;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;,&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;<a href="http://proxyip.snowdreams1006.cn/get/" target="_blank">http://proxyip.snowdreams1006.cn/get/</a>&#x9A8C;&#x8BC1;&#x662F;&#x5426;&#x80FD;&#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406; ip,&#x6216;&#x8005;&#x5728;&#x7EC8;&#x7AEF;&#x547D;&#x4EE4;&#x884C;&#x8FD0;&#x884C; <code>curl http://proxyip.snowdreams1006.cn/get/</code> &#x547D;&#x4EE4;&#x67E5;&#x770B;&#x7ED3;&#x679C;.</p>
</blockquote>
<h5 id="&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;ip&#x8BBF;&#x95EE;"><a name="&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;ip&#x8BBF;&#x95EE;" class="anchor-navigation-ex-anchor" href="#&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;ip&#x8BBF;&#x95EE;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;ip&#x8BBF;&#x95EE;</h5>
<blockquote>
<p><code>urllib.FancyURLopener(proxy)</code> : &#x8BBE;&#x7F6E;&#x4EE3;&#x7406; ip &#x4FE1;&#x606F;&#x5B9E;&#x73B0;&#x95F4;&#x63A5;&#x8BBF;&#x95EE;</p>
</blockquote>
<p>&#x901A;&#x8FC7; <code>urllib.FancyURLopener(proxy)</code> &#x53EF;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;,&#x7528;&#x4E8E;&#x5411;&#x670D;&#x52A1;&#x7AEF;&#x9690;&#x85CF;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x771F;&#x5B9E;&#x4FE1;&#x606F;,&#x4F46;&#x662F;&#x670D;&#x52A1;&#x7AEF;&#x5230;&#x5E95;&#x80FD;&#x5426;&#x533A;&#x5206;&#x4EE3;&#x7406;&#x8BF7;&#x6C42;&#x548C;&#x666E;&#x901A;&#x8BF7;&#x6C42;&#x662F;&#x548C;&#x4EE3;&#x7406; ip &#x6709;&#x5173;&#x7684;.</p>
<p>&#x5982;&#x679C;&#x662F;&#x9AD8;&#x533F;&#x4EE3;&#x7406;&#x7684;&#x8BDD;,&#x662F;&#x6700;&#x4E3A;&#x7406;&#x60F3;&#x7684;&#x4E00;&#x79CD;&#x60C5;&#x51B5;,&#x80FD;&#x591F;&#x8FBE;&#x5230;&#x771F;&#x6B63;&#x4EE3;&#x7406;&#x7684;&#x4F5C;&#x7528;.</p>
<p>&#x76F8;&#x53CD;,&#x5982;&#x679C;&#x662F;&#x900F;&#x660E;&#x4EE3;&#x7406;&#x7684;&#x8BDD;,&#x662F;&#x6700;&#x6CA1;&#x7528;&#x7684;&#x4EE3;&#x7406;,&#x670D;&#x52A1;&#x7AEF;&#x4E0D;&#x4EC5;&#x77E5;&#x9053;&#x4F60;&#x6B63;&#x5728;&#x4F7F;&#x7528;&#x4EE3;&#x7406;&#x8FD8;&#x77E5;&#x9053;&#x4F60;&#x771F;&#x5B9E; ip,&#x6709;&#x4E00;&#x79CD;&#x63A9;&#x8033;&#x76D7;&#x94C3;&#x7684;&#x9519;&#x89C9;.</p>
<p>&#x5982;&#x4F55;&#x9A8C;&#x8BC1;&#x8BBE;&#x7F6E;&#x7684;&#x4EE3;&#x7406; ip &#x662F;&#x5426;&#x80FD;&#x88AB;&#x670D;&#x52A1;&#x7AEF;&#x8BC6;&#x522B;,&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;<a href="http://httpbin.snowdreams1006.cn/ip" target="_blank">http://httpbin.snowdreams1006.cn/ip</a>&#x83B7;&#x53D6;&#x670D;&#x52A1;&#x7AEF;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x5BA2;&#x6237;&#x7AEF; ip.</p>
<pre><code class="lang-bash">$ curl http://httpbin.snowdreams1006.cn/ip
{
  <span class="hljs-string">&quot;origin&quot;</span>: <span class="hljs-string">&quot;115.217.104.191&quot;</span>
}
</code></pre>
<blockquote>
<p>&#x5982;&#x679C;&#x7EC8;&#x7AEF;&#x547D;&#x4EE4;&#x884C;&#x6CA1; <code>curl</code> &#x547D;&#x4EE4;,&#x53EF;&#x4EE5;&#x767E;&#x5EA6;&#x4E00;&#x4E0B;&#x81EA;&#x884C;&#x5B89;&#x88C5;&#x6216;&#x8005;&#x76F4;&#x63A5;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x8BBF;&#x95EE;<a href="http://httpbin.snowdreams1006.cn/ip" target="_blank">http://httpbin.snowdreams1006.cn/ip</a></p>
</blockquote>
<p>&#x5982;&#x679C;&#x670D;&#x52A1;&#x5668;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x8BF7;&#x6C42; ip &#x548C;&#x8BBE;&#x7F6E;&#x7684;&#x4EE3;&#x7406; ip &#x4E00;&#x81F4;,&#x606D;&#x559C;&#x4F60;,&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;&#x6210;&#x529F;&#x800C;&#x4E14;&#x662F;&#x9AD8;&#x533F;&#x4EE3;&#x7406;,&#x5426;&#x5219;&#x7684;&#x8BDD;,&#x90A3;&#x5C31;&#x662F;<strong>&#x63A9;&#x8033;&#x76D7;&#x94C3;</strong>&#x7F62;&#x4E86;.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2
<span class="hljs-keyword">import</span> json

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_proxy</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://proxyip.snowdreams1006.cn/get/&apos;</span>)
    result = response.read()
    <span class="hljs-keyword">return</span> json.loads(result)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_proxy_urllib</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x901A;&#x8FC7;&#x4EE3;&#x7406;&#x53D1;&#x9001;&#x8BF7;&#x6C42;
    &apos;&apos;&apos;</span>
    <span class="hljs-comment"># &#x968F;&#x673A;&#x4EE3;&#x7406; ip</span>
    ip = get_proxy().get(<span class="hljs-string">&apos;proxy&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Get Proxy:&apos;</span>)
    print(ip)
    proxy = {
        <span class="hljs-string">&apos;http&apos;</span>: <span class="hljs-string">&apos;http://{}&apos;</span>.format(ip),
        <span class="hljs-string">&apos;https&apos;</span>: <span class="hljs-string">&apos;https://{}&apos;</span>.format(ip)
    }
    opener = urllib.FancyURLopener(proxy)
    response = opener.open()
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Get proxy urllib&lt;&lt;&lt;&apos;</span>
    get_proxy_urllib()
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x793A;&#x4F8B;&#x53EA;&#x662F;&#x6F14;&#x793A;<strong>&#x5982;&#x4F55;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406; ip &#x53D1;&#x9001;&#x8BF7;&#x6C42;</strong>,&#x5E76;&#x6CA1;&#x6709;<strong>&#x9A8C;&#x8BC1;&#x4EE3;&#x7406; ip &#x662F;&#x5426;&#x8BBE;&#x7F6E;&#x6210;&#x529F;</strong>,&#x5373;&#x670D;&#x52A1;&#x7AEF;&#x8BFB;&#x53D6;&#x5230;&#x8BF7;&#x6C42; ip &#x548C;&#x521A;&#x521A;&#x8BBE;&#x7F6E;&#x7684;&#x4EE3;&#x7406; ip&#x662F;&#x5426;&#x76F8;&#x540C;,&#x5E76;&#x4E14;&#x4E5F;&#x6CA1;&#x6709;&#x8003;&#x8651;&#x4EE3;&#x7406; ip &#x4E0D;&#x53EF;&#x7528;&#x6216;&#x8005;&#x8FDE;&#x63A5;&#x8D85;&#x65F6;&#x7B49;<strong>&#x5F02;&#x5E38;&#x60C5;&#x51B5;</strong>.</p>
<p>&#x4E0B;&#x9762;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x793A;&#x4F8B;<strong>&#x5224;&#x65AD;&#x4EE3;&#x7406; ip &#x662F;&#x5426;&#x8BBE;&#x7F6E;&#x6210;&#x529F;</strong>:</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;proxy&quot;</span>: <span class="hljs-string">&quot;121.225.199.78:3128&quot;</span>, 
    <span class="hljs-string">&quot;fail_count&quot;</span>: <span class="hljs-number">0</span>, 
    <span class="hljs-string">&quot;region&quot;</span>: <span class="hljs-string">&quot;&quot;</span>, 
    <span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-string">&quot;source&quot;</span>: 
    <span class="hljs-string">&quot;freeProxy09&quot;</span>, 
    <span class="hljs-string">&quot;check_count&quot;</span>: <span class="hljs-number">15</span>, 
    <span class="hljs-string">&quot;last_status&quot;</span>: <span class="hljs-number">1</span>, 
    <span class="hljs-string">&quot;last_time&quot;</span>: <span class="hljs-string">&quot;2020-01-17 12:03:29&quot;</span>
}
</code></pre>
<blockquote>
<p>&#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406; ip &#x7684;&#x4E00;&#x822C;&#x683C;&#x5F0F;,&#x63D0;&#x53D6;&#x51FA;&#x968F;&#x673A; ip &#x7684;&#x4E00;&#x822C;&#x503C;&#x4E3A; <code>121.225.199.78:3128</code></p>
</blockquote>
<p>&#x9488;&#x5BF9;&#x968F;&#x673A;&#x83B7;&#x53D6;&#x4EE3;&#x7406;ip &#x7684;&#x4E00;&#x822C;&#x683C;&#x5F0F;&#x662F;&#x5E26;&#x6709;&#x7AEF;&#x53E3;&#x53F7;,&#x800C;&#x8BBF;&#x95EE; <a href="http://httpbin.snowdreams1006.cn/ip" target="_blank">http://httpbin.snowdreams1006.cn/ip</a> &#x83B7;&#x53D6;&#x5230;&#x6765;&#x6E90; ip &#x5E76;&#x4E0D;&#x5305;&#x62EC;&#x7AEF;&#x53E3;&#x53F7;,&#x56E0;&#x6B64;&#x6700;&#x7B80;&#x5355;&#x7684;&#x601D;&#x8DEF;&#x662F;&#x53BB;&#x6389;&#x968F;&#x673A; ip &#x7684;&#x7AEF;&#x53E3;&#x53F7;,&#x7136;&#x540E;&#x518D;&#x548C;&#x8BBF;&#x95EE;&#x7ED3;&#x679C;&#x4F5C;&#x6BD4;&#x8F83;.</p>
<pre><code class="lang-python"><span class="hljs-string">&apos;121.225.199.78:3128&apos;</span>.split(<span class="hljs-string">&apos;:&apos;</span>)[<span class="hljs-number">0</span>]
</code></pre>
<blockquote>
<p>&#x9996;&#x5148;&#x4EE5; <code>:</code> &#x5206;&#x5272;&#x6210;&#x4E24;&#x90E8;&#x5206;,&#x7136;&#x540E;&#x53EA;&#x53D6;&#x7B2C;&#x4E00;&#x90E8;&#x5206;,&#x5373;&#x83B7;&#x53D6;&#x4E0D;&#x5E26;&#x7AEF;&#x53E3;&#x53F7;&#x7684; ip &#x5730;&#x5740;: <code>121.225.199.78</code></p>
</blockquote>
<p>&#x63A5;&#x4E0B;&#x6765;&#x7531;&#x4E8E; <code>response.read()</code> &#x83B7;&#x53D6;&#x5230;&#x7684;&#x54CD;&#x5E94;&#x4F53;&#x6570;&#x636E;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;,&#x4E0D;&#x65B9;&#x4FBF;&#x63D0;&#x53D6;&#x51FA;&#x5176;&#x4E2D; <code>origin</code> &#x5BF9;&#x5E94;&#x7684;&#x503C;,&#x800C;&#x54CD;&#x5E94;&#x4F53;&#x6570;&#x636E;&#x660E;&#x663E;&#x53C8;&#x662F; <code>json</code> &#x683C;&#x5F0F;,&#x56E0;&#x6B64;&#x4F7F;&#x7528; <code>json.loads(result)</code> &#x53EF;&#x65B9;&#x4FBF;&#x8F6C;&#x6362;&#x6210; <code>python</code> &#x7684;&#x5B57;&#x5178;&#x7C7B;&#x578B;.</p>
<pre><code class="lang-python">result = response.read()
result = json.loads(result)
proxyip = result.get(<span class="hljs-string">&apos;origin&apos;</span>)
</code></pre>
<blockquote>
<p>&#x9488;&#x5BF9;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#x7684;&#x53D6;&#x503C;&#x65B9;&#x5F0F;&#x4E0D;&#x4EC5;&#x4EC5;&#x53EF;&#x4EE5; <code>result.get(&apos;origin&apos;)</code> &#x4E5F;&#x53EF;&#x4EE5; <code>result[&apos;origin&apos;]</code> ,&#x53EA;&#x4E0D;&#x8FC7;&#x5F53;&#x952E;&#x540D;&#x4E0D;&#x5B58;&#x5728;&#x65F6;&#x4E24;&#x8005;&#x7684;&#x8868;&#x73B0;&#x4E0D;&#x4E00;&#x81F4;,<strong>&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x53D6;&#x503C;</strong>.</p>
</blockquote>
<p>&#x73B0;&#x5728;&#x6700;&#x7B80;&#x5355;&#x9A8C;&#x8BC1;&#x4EE3;&#x7406; ip &#x662F;&#x5426;&#x8BBE;&#x7F6E;&#x6210;&#x529F;&#x7684;&#x5B8C;&#x6574;&#x793A;&#x4F8B;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2
<span class="hljs-keyword">import</span> json

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_proxy</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://proxyip.snowdreams1006.cn/get/&apos;</span>)
    result = response.read()
    <span class="hljs-keyword">return</span> json.loads(result)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_proxy_urllib</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x901A;&#x8FC7;&#x4EE3;&#x7406;&#x53D1;&#x9001;&#x8BF7;&#x6C42;
    &apos;&apos;&apos;</span>
    <span class="hljs-comment"># &#x968F;&#x673A;&#x4EE3;&#x7406; ip</span>
    ip = get_proxy().get(<span class="hljs-string">&apos;proxy&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Get Proxy:&apos;</span>)
    print(ip)
    proxy = {
        <span class="hljs-string">&apos;http&apos;</span>: <span class="hljs-string">&apos;http://{}&apos;</span>.format(ip),
        <span class="hljs-string">&apos;https&apos;</span>: <span class="hljs-string">&apos;https://{}&apos;</span>.format(ip)
    }
    opener = urllib.FancyURLopener(proxy)
    response = opener.open()
    result = response.read()
    result = json.loads(result)
    response_ip = result.get(<span class="hljs-string">&apos;origin&apos;</span>)
    proxy_ip = ip.split(<span class="hljs-string">&apos;:&apos;</span>)[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">if</span> proxy_ip == response_ip:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Proxy Success&apos;</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Proxy Fail&apos;</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Get proxy urllib&lt;&lt;&lt;&apos;</span>
    get_proxy_urllib()
</code></pre>
<p>&#x5982;&#x679C;&#x968F;&#x673A;&#x83B7;&#x53D6;&#x7684;&#x4EE3;&#x7406; ip &#x6B63;&#x5E38;&#x7684;&#x8BDD;,&#x4E00;&#x822C;&#x4E0D;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;,&#x8981;&#x4E48;&#x8BBE;&#x7F6E;&#x6210;&#x529F;,&#x8981;&#x4E48;&#x8BBE;&#x7F6E;&#x5931;&#x8D25;.</p>
<pre><code class="lang-bash">(.env) $ python urllib_demo.py 
&gt;&gt;&gt;Get proxy urllib&lt;&lt;&lt;
&gt;&gt;&gt;Get Proxy:
52.80.58.248:3128
Proxy Fail

(.env) $ python urllib_demo.py 
&gt;&gt;&gt;Get proxy urllib&lt;&lt;&lt;
&gt;&gt;&gt;Get Proxy:
117.88.176.152:3000
Proxy Success
</code></pre>
<blockquote>
<p>&#x514D;&#x8D39;&#x4EE3;&#x7406; ip &#x8D28;&#x91CF;&#x4E00;&#x822C;&#x822C;&#x800C;&#x5DF2;,&#x56E0;&#x6B64;&#x4E5F;&#x4E0D;&#x8981;&#x62B1;&#x6709;&#x592A;&#x9AD8;&#x7684;&#x5FC3;&#x7406;&#x9884;&#x671F;,&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#x8FD8;&#x662F;&#x5E94;&#x8BE5;&#x9009;&#x62E9;<strong>&#x4ED8;&#x8D39;&#x4EE3;&#x7406; ip</strong>.</p>
</blockquote>
<h5 id="&#x6E05;&#x9664;&#x4EE3;&#x7406;-ip-&#x76F4;&#x8FDE;"><a name="&#x6E05;&#x9664;&#x4EE3;&#x7406;-ip-&#x76F4;&#x8FDE;" class="anchor-navigation-ex-anchor" href="#&#x6E05;&#x9664;&#x4EE3;&#x7406;-ip-&#x76F4;&#x8FDE;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6E05;&#x9664;&#x4EE3;&#x7406; ip &#x76F4;&#x8FDE;</h5>
<blockquote>
<p><code>urllib.FancyURLopener({})</code> : &#x6E05;&#x9664;&#x4EE3;&#x7406; ip &#x4FE1;&#x606F;&#x5B9E;&#x73B0;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;</p>
</blockquote>
<p>&#x8BBE;&#x7F6E;&#x4EE3;&#x7406; ip &#x65F6;&#x9700;&#x8981;&#x4F20;&#x9012;&#x7ED9; <code>urllib.FancyURLopener(proxy)</code> &#x4E00;&#x4E2A;&#x4EE3;&#x7406;&#x5B57;&#x5178;,&#x6E05;&#x9664;&#x4EE3;&#x7406;&#x4FE1;&#x606F;&#x65F6;&#x53EA;&#x9700;&#x8981;&#x5C06;&#x539F;&#x6765;&#x7684;&#x4EE3;&#x7406;&#x5B57;&#x5178;&#x8BBE;&#x7F6E;&#x6210;<strong>&#x7A7A;&#x5BF9;&#x8C61;</strong>&#x5373;&#x53EF;.</p>
<p>&#x4E3B;&#x8981;&#x4EE3;&#x7801;&#x548C;&#x8BBE;&#x7F6E;&#x4EE3;&#x7406; ip &#x76F8;&#x5DEE;&#x65E0;&#x4E8C;,&#x4E0D;&#x518D;&#x8D58;&#x8FF0;,&#x53EF;&#x53C2;&#x8003;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2
<span class="hljs-keyword">import</span> json

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">clear_proxy_urllib</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x6E05;&#x9664;&#x4EE3;&#x7406;&#x540E;&#x53D1;&#x9001;&#x8BF7;&#x6C42;
    &apos;&apos;&apos;</span>
    <span class="hljs-comment"># &#x968F;&#x673A;&#x4EE3;&#x7406; ip</span>
    ip = get_proxy().get(<span class="hljs-string">&apos;proxy&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Get Proxy:&apos;</span>)
    print(ip)
    proxy = {
        <span class="hljs-string">&apos;http&apos;</span>: <span class="hljs-string">&apos;http://{}&apos;</span>.format(ip),
        <span class="hljs-string">&apos;https&apos;</span>: <span class="hljs-string">&apos;https://{}&apos;</span>.format(ip)
    }
    opener = urllib.FancyURLopener(proxy)
    response = opener.open(<span class="hljs-string">&quot;http://httpbin.snowdreams1006.cn/ip&quot;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    result = response.read()
    print(result)
    result = json.loads(result)
    response_ip = result.get(<span class="hljs-string">&apos;origin&apos;</span>)
    proxy_ip = ip.split(<span class="hljs-string">&apos;:&apos;</span>)[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">if</span> proxy_ip == response_ip:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Set proxy success&apos;</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Set proxy fail&apos;</span>

    opener = urllib.FancyURLopener({})
    response = opener.open(<span class="hljs-string">&quot;http://httpbin.snowdreams1006.cn/ip&quot;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    result = response.read()
    print(result)
    result = json.loads(result)
    response_ip = result.get(<span class="hljs-string">&apos;origin&apos;</span>)
    proxy_ip = ip.split(<span class="hljs-string">&apos;:&apos;</span>)[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">if</span> proxy_ip == response_ip:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Clear proxy fail&apos;</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Clear proxy success&apos;</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Get proxy urllib&lt;&lt;&lt;&apos;</span>
    get_proxy_urllib()
</code></pre>
<p>&#x9664;&#x4E86;&#x4E0A;&#x8FF0;&#x65B9;&#x6CD5;&#x4F7F;&#x7528; <code>urllib.FancyURLopener()</code> <strong>&#x8BBE;&#x7F6E;&#x6216;&#x6E05;&#x9664;&#x4EE3;&#x7406;</strong> ip,&#x5176;&#x5B9E;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>urllib.urlopen()</code> &#x5B9E;&#x73B0;&#x7C7B;&#x4F3C;&#x9700;&#x6C42;.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Use http://www.someproxy.com:3128 for HTTP proxying</span>
proxies = {<span class="hljs-string">&apos;http&apos;</span>: <span class="hljs-string">&apos;http://www.someproxy.com:3128&apos;</span>}
filehandle = urllib.urlopen(some_url, proxies=proxies)

<span class="hljs-comment"># Don&apos;t use any proxies</span>
filehandle = urllib.urlopen(some_url, proxies={})

<span class="hljs-comment"># Use proxies from environment - both versions are equivalent</span>
filehandle = urllib.urlopen(some_url, proxies=<span class="hljs-keyword">None</span>)
filehandle = urllib.urlopen(some_url)
</code></pre>
<p>&#x5176;&#x4E2D;&#x5173;&#x4E8E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x8BBE;&#x7F6E;&#x793A;&#x4F8B;,&#x5982;&#x4E0B;:</p>
<pre><code class="lang-bash">% http_proxy=<span class="hljs-string">&quot;http://www.someproxy.com:3128&quot;</span>
% <span class="hljs-built_in">export</span> http_proxy
% python
...
</code></pre>
<h2 id="&#x5B66;&#x4E60;&#x603B;&#x7ED3;"><a name="&#x5B66;&#x4E60;&#x603B;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x5B66;&#x4E60;&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. &#x5B66;&#x4E60;&#x603B;&#x7ED3;</h2>
<p>&#x672C;&#x6587;&#x4E3B;&#x8981;&#x4ECB;&#x7ECD;&#x4E86; <code>python</code> &#x4E2D;&#x539F;&#x751F;&#x7684; <code>urllib</code> &#x5982;&#x4F55;<strong>&#x53D1;&#x9001;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;</strong>&#x4EE5;&#x53CA;&#x4E00;&#x4E9B;&#x57FA;&#x672C;&#x73AF;&#x5883;&#x7684;&#x642D;&#x5EFA;&#x8FC7;&#x7A0B;,&#x5176;&#x4E2D;&#x9644;&#x5E26;&#x5927;&#x91CF;&#x53EF;&#x76F4;&#x63A5;&#x64CD;&#x4F5C;&#x7684;<strong>&#x73B0;&#x6210;&#x4EE3;&#x7801;</strong>,&#x6587;&#x6863;&#x548C;&#x6E90;&#x7801;&#x5747;&#x5DF2;<strong>&#x5F00;&#x6E90;</strong>,&#x611F;&#x5174;&#x8DA3;&#x7684;&#x5C0F;&#x4F19;&#x4F34;&#x53EF;&#x81EA;&#x884C;<a href="https://github.com/snowdreams1006/learn-python/tree/master/src/url/urllib" target="_blank">&#x7FFB;&#x9605;&#x6D4F;&#x89C8;</a>.</p>
<p><img src="images/url-urllib-summary-github-preview.png" alt="url-urllib-summary-github-preview.png"></p>
<p>&#x73B0;&#x5728;&#x7B80;&#x8981;&#x56DE;&#x987E;&#x4E00;&#x4E0B;&#x672C;&#x6587;&#x4E3B;&#x8981;&#x6D89;&#x53CA;&#x5230;&#x7684;&#x91CD;&#x8981;&#x77E5;&#x8BC6;&#x70B9;,&#x4EE5;&#x4F9B;&#x540E;&#x6765;&#x8005;&#x5B66;&#x4E60;&#x65F6;&#x5FEB;&#x901F;&#x7FFB;&#x9605;&#x67E5;&#x8BE2;.</p>
<h3 id="&#x865A;&#x62DF;&#x73AF;&#x5883;-virtualenv"><a name="&#x865A;&#x62DF;&#x73AF;&#x5883;-virtualenv" class="anchor-navigation-ex-anchor" href="#&#x865A;&#x62DF;&#x73AF;&#x5883;-virtualenv"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.1. &#x865A;&#x62DF;&#x73AF;&#x5883; virtualenv</h3>
<p>&#x865A;&#x62DF;&#x73AF;&#x5883;&#x5B89;&#x88C5;&#x5E76;&#x6FC0;&#x6D3B;&#x6210;&#x529F;&#x540E;,<code>python</code> &#x548C; <code>pip</code> &#x7684;&#x7248;&#x672C;&#x4FE1;&#x606F;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-bash">(.env)$ python --version
Python 2.7.16
(.env) $ pip --version
pip 19.3.1 from ~/python/src/url/urllib/.env/lib/python2.7/site-packages/pip (python 2.7)
</code></pre>
<p>&#x5982;&#x9700;&#x81EA;&#x884C;&#x642D;&#x5EFA;&#x8BE5;&#x865A;&#x62DF;&#x73AF;&#x5883;,&#x53EF;&#x53C2;&#x8003;&#x4EE5;&#x4E0B;&#x51E0;&#x6B65;&#x5F00;&#x542F;&#x865A;&#x62DF;&#x73AF;&#x5883;:</p>
<ul>
<li>&#x6B65;&#x9AA4;1. &#x5B89;&#x88C5;&#x865A;&#x62DF;&#x73AF;&#x5883; <code>virtualenv</code> </li>
</ul>
<pre><code class="lang-bash">sudo pip install virtualenv
</code></pre>
<blockquote>
<p>&#x5B89;&#x88C5;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x65B9;&#x4FBF;&#x9694;&#x79BB;&#x4E0D;&#x540C; python &#x73AF;&#x5883;,&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x73AF;&#x5883;</strong>,&#x6240;&#x4EE5;&#x8FD9;&#x4E00;&#x6B65;&#x662F;&#x53EF;&#x9009;&#x7684;,&#x540C;&#x7406;&#x4E0B;&#x9762;&#x7684;&#x6B65;&#x9AA4;&#x4E5F;&#x90FD;&#x662F;&#x53EF;&#x9009;&#x7684;.</p>
</blockquote>
<ul>
<li>&#x6B65;&#x9AA4;2. &#x51C6;&#x5907;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x76EE;&#x5F55; <code>.env</code></li>
</ul>
<pre><code class="lang-bash">virtualenv .env
</code></pre>
<blockquote>
<p>&#x865A;&#x62DF;&#x73AF;&#x5883;&#x76EE;&#x5F55;&#x8BBE;&#x7F6E;&#x6210;&#x9690;&#x85CF;&#x76EE;&#x5F55;&#x7684;&#x76EE;&#x7684;&#x662F;&#x9632;&#x6B62;&#x8BEF;&#x64CD;&#x4F5C;,&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x6210;&#x666E;&#x901A;&#x76EE;&#x5F55;&#x90A3;&#x6837;&#x663E;&#x793A;&#x51FA;&#x6765;.</p>
</blockquote>
<ul>
<li>&#x6B65;&#x9AA4;3. &#x6FC0;&#x6D3B;&#x865A;&#x62DF;&#x73AF;&#x5883; <code>.env</code></li>
</ul>
<pre><code class="lang-bash"><span class="hljs-built_in">source</span> .env/bin/activate
</code></pre>
<blockquote>
<p>&#x6FC0;&#x6D3B;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x540E;&#x53EF;&#x4EE5;&#x8FD0;&#x884C; <code>pip --version</code> &#x67E5;&#x770B;&#x5F53;&#x524D;&#x7248;&#x672C;&#x4FE1;&#x606F;,&#x7531;&#x6B64;&#x9A8C;&#x8BC1;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x662F;&#x5426;&#x5F00;&#x542F;&#x6210;&#x529F;.</p>
</blockquote>
<h3 id="&#x670D;&#x52A1;&#x7AEF;&#x540E;&#x53F0;-httpbin"><a name="&#x670D;&#x52A1;&#x7AEF;&#x540E;&#x53F0;-httpbin" class="anchor-navigation-ex-anchor" href="#&#x670D;&#x52A1;&#x7AEF;&#x540E;&#x53F0;-httpbin"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.2. &#x670D;&#x52A1;&#x7AEF;&#x540E;&#x53F0; httpbin</h3>
<blockquote>
<p>&#x9ED8;&#x8BA4;&#x672C;&#x5730;&#x8BBF;&#x95EE;&#x5730;&#x5740;: <a href="http://127.0.0.1:8000/" target="_blank">http://127.0.0.1:8000/</a>,&#x7EBF;&#x4E0A;&#x8BBF;&#x95EE;&#x5730;&#x5740;: <a href="http://httpbin.snowdreams1006.cn/" target="_blank">http://httpbin.snowdreams1006.cn/</a> &#x6216;&#x8005; <a href="http://httpbin.org/" target="_blank">http://httpbin.org/</a></p>
</blockquote>
<p>&#x5982;&#x679C;&#x91C7;&#x7528; <code>docker</code> &#x5B89;&#x88C5; <code>httpbin</code> &#x8FD0;&#x884C;&#x6210;&#x529F;&#x540E;,&#x8BBF;&#x95EE;&#x63A5;&#x53E3;&#x5730;&#x5740;,&#x5B9E;&#x9645;&#x9884;&#x89C8;&#x5982;&#x4E0B;:</p>
<p><img src="images/url-urllib-httpbin-docker-preview.png" alt="url-urllib-httpbin-docker-preview.png"></p>
<p>&#x5982;&#x679C;&#x4F7F;&#x7528; <code>python</code> &#x542F;&#x52A8; <code>httpbin</code> &#x5E93;,&#x8FD0;&#x884C;&#x6210;&#x529F;&#x540E;&#x6548;&#x679C;&#x548C; <code>docker</code> &#x65B9;&#x5F0F;&#x6709;&#x6240;&#x4E0D;&#x540C;,&#x6548;&#x679C;&#x5982;&#x4E0B;:</p>
<p><img src="images/url-urllib-httpbin-pip-preview.png" alt="url-urllib-httpbin-pip-preview.png"></p>
<p>&#x5982;&#x9700;&#x81EA;&#x884C;&#x642D;&#x5EFA;&#x672C;&#x5730;&#x670D;&#x52A1;,&#x8BF7;&#x8BFB;&#x8005;&#x6839;&#x636E;&#x81EA;&#x8EAB;&#x9700;&#x8981;&#x81EA;&#x884C;&#x51B3;&#x5B9A;&#x5B89;&#x88C5;&#x65B9;&#x5F0F;,&#x4E0B;&#x9762;&#x63D0;&#x4F9B;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x5F00;&#x542F; <code>httpbin</code> &#x670D;&#x52A1;.</p>
<ul>
<li><code>docker</code> &#x5B89;&#x88C5;&#x65B9;&#x5F0F;</li>
</ul>
<pre><code class="lang-bash">docker run -p 8000:80 kennethreitz/httpbin
</code></pre>
<blockquote>
<p>&#x9996;&#x6B21;&#x8FD0;&#x884C;&#x4F1A;&#x5148;&#x5C06;&#x955C;&#x50CF;&#x4E0B;&#x8F7D;&#x5230;&#x672C;&#x5730;&#x518D;&#x542F;&#x52A8;&#x5BB9;&#x5668;,&#x975E;&#x9996;&#x6B21;&#x8FD0;&#x884C;&#x4F1A;&#x76F4;&#x63A5;&#x542F;&#x52A8;&#x5BB9;&#x5668;,&#x8BBF;&#x95EE;&#x5730;&#x5740;: <a href="http://127.0.0.1:8000/" target="_blank">http://127.0.0.1:8000/</a></p>
</blockquote>
<ul>
<li><code>python</code> &#x5B89;&#x88C5;&#x65B9;&#x5F0F;</li>
</ul>
<pre><code class="lang-bash">pip install gunicorn httpbin &amp;&amp; gunicorn httpbin:app
</code></pre>
<blockquote>
<p>&#x9ED8;&#x8BA4;&#x76D1;&#x542C;&#x7AEF;&#x53E3;&#x662F; <code>8000</code>,&#x5982;&#x679C;&#x9047;&#x5230;&#x7AEF;&#x53E3;&#x51B2;&#x7A81;&#x63D0;&#x793A;&#x5DF2;&#x88AB;&#x5360;&#x7528;,&#x53EF;&#x8FD0;&#x884C; <code>gunicorn httpbin:app -b :9898</code> &#x6307;&#x5B9A;&#x7AEF;&#x53E3;.</p>
</blockquote>
<h3 id="&#x514D;&#x8D39;ip&#x4EE3;&#x7406;&#x6C60;-proxyip"><a name="&#x514D;&#x8D39;ip&#x4EE3;&#x7406;&#x6C60;-proxyip" class="anchor-navigation-ex-anchor" href="#&#x514D;&#x8D39;ip&#x4EE3;&#x7406;&#x6C60;-proxyip"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.3. &#x514D;&#x8D39;ip&#x4EE3;&#x7406;&#x6C60; proxyip</h3>
<blockquote>
<p>&#x9ED8;&#x8BA4;&#x672C;&#x5730;&#x8BBF;&#x95EE;&#x5730;&#x5740;: <a href="http://127.0.0.1:5010/" target="_blank">http://127.0.0.1:5010/</a>,&#x7EBF;&#x4E0A;&#x8BBF;&#x95EE;&#x5730;&#x5740;: <a href="http://proxyip.snowdreams1006.cn/" target="_blank">http://proxyip.snowdreams1006.cn/</a> &#x6216;&#x8005; <a href="http://118.24.52.95/" target="_blank">http://118.24.52.95/</a></p>
</blockquote>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;delete?proxy=127.0.0.1:8080&quot;</span>: <span class="hljs-string">&quot;delete an unable proxy&quot;</span>, 
  <span class="hljs-string">&quot;get&quot;</span>: <span class="hljs-string">&quot;get an useful proxy&quot;</span>, 
  <span class="hljs-string">&quot;get_all&quot;</span>: <span class="hljs-string">&quot;get all proxy from proxy pool&quot;</span>, 
  <span class="hljs-string">&quot;get_status&quot;</span>: <span class="hljs-string">&quot;proxy number&quot;</span>
}
</code></pre>
<p>&#x5982;&#x9700;&#x81EA;&#x884C;&#x642D;&#x5EFA;&#x672C;&#x5730;&#x670D;&#x52A1;,&#x8BF7;&#x8BFB;&#x8005;&#x6839;&#x636E;&#x81EA;&#x8EAB;&#x9700;&#x8981;&#x81EA;&#x884C;&#x51B3;&#x5B9A;&#x5B89;&#x88C5;&#x65B9;&#x5F0F;,&#x4E0B;&#x9762;&#x63D0;&#x4F9B;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x5F00;&#x542F; <code>proxyip</code> &#x670D;&#x52A1;.</p>
<ul>
<li><code>docker</code> &#x5B89;&#x88C5;&#x65B9;&#x5F0F;</li>
</ul>
<pre><code class="lang-bash">docker run --env db_<span class="hljs-built_in">type</span>=REDIS --env db_host=127.0.0.1 --env db_port=6379 --env db_password=<span class="hljs-string">&apos;&apos;</span> -p 5010:5010 jhao104/proxy_pool
</code></pre>
<blockquote>
<p>&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x63D0;&#x524D;&#x4E0B;&#x8F7D;&#x955C;&#x50CF;: <code>docker pull jhao104/proxy_pool</code>,&#x7136;&#x540E;&#x518D;&#x8FD0;&#x884C;&#x4E0A;&#x8FF0;&#x547D;&#x4EE4;&#x542F;&#x52A8;&#x5BB9;&#x5668;.</p>
</blockquote>
<ul>
<li><p>&#x6E90;&#x7801;&#x5B89;&#x88C5;&#x65B9;&#x5F0F;</p>
<ul>
<li><p>&#x6B65;&#x9AA4; 1 : &#x4E0B;&#x8F7D;&#x6E90;&#x7801;</p>
<pre><code class="lang-bash">git <span class="hljs-built_in">clone</span> https://github.com/jhao104/proxy_pool.git
</code></pre>
<blockquote>
<p>&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4E0B;&#x8F7D;&#x5B89;&#x88C5;&#x5305;: <a href="https://github.com/jhao104/proxy_pool/releases" target="_blank">https://github.com/jhao104/proxy_pool/releases</a> </p>
</blockquote>
</li>
<li><p>&#x6B65;&#x9AA4; 2 : &#x5B89;&#x88C5;&#x4F9D;&#x8D56;</p>
<pre><code class="lang-bash">pip install -r requirements.txt
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;: &#x5B89;&#x88C5;&#x9879;&#x76EE;&#x4F9D;&#x8D56;&#x65F6;&#x9700;&#x8981;&#x63D0;&#x524D;&#x5207;&#x6362;&#x5230;&#x9879;&#x76EE;&#x6839;&#x76EE;&#x5F55;(<code>cd proxy_pool</code>),&#x5982;&#x679C;&#x5ACC;&#x5F03;&#x4E0B;&#x8F7D;&#x901F;&#x5EA6;&#x6162;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6E05;&#x534E;&#x5927;&#x5B66;&#x955C;&#x50CF; <code>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt</code> &#x52A0;&#x901F;&#x4E0B;&#x8F7D;&#x5B89;&#x88C5;&#x8FC7;&#x7A0B;.</p>
</blockquote>
</li>
<li><p>&#x6B65;&#x9AA4; 3 : &#x914D;&#x7F6E; <code>Config/setting.py</code></p>
<pre><code class="lang-python"><span class="hljs-comment"># Config/setting.py &#x4E3A;&#x9879;&#x76EE;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span>

<span class="hljs-comment"># &#x914D;&#x7F6E;DB     </span>
DATABASES = {
  <span class="hljs-string">&quot;default&quot;</span>: {
      <span class="hljs-string">&quot;TYPE&quot;</span>: <span class="hljs-string">&quot;REDIS&quot;</span>,        <span class="hljs-comment"># &#x76EE;&#x524D;&#x652F;&#x6301;SSDB&#x6216;REDIS&#x6570;&#x636E;&#x5E93;</span>
      <span class="hljs-string">&quot;HOST&quot;</span>: <span class="hljs-string">&quot;127.0.0.1&quot;</span>,   <span class="hljs-comment"># db host</span>
      <span class="hljs-string">&quot;PORT&quot;</span>: <span class="hljs-number">6379</span>,          <span class="hljs-comment"># db port&#xFF0C;&#x4F8B;&#x5982;SSDB&#x901A;&#x5E38;&#x4F7F;&#x7528;8888&#xFF0C;REDIS&#x901A;&#x5E38;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;6379</span>
      <span class="hljs-string">&quot;NAME&quot;</span>: <span class="hljs-string">&quot;proxy&quot;</span>,       <span class="hljs-comment"># &#x9ED8;&#x8BA4;&#x914D;&#x7F6E;</span>
      <span class="hljs-string">&quot;PASSWORD&quot;</span>: <span class="hljs-string">&quot;&quot;</span>         <span class="hljs-comment"># db password</span>
  }
}

<span class="hljs-comment"># &#x914D;&#x7F6E; API&#x670D;&#x52A1;</span>
SERVER_API = {
  <span class="hljs-string">&quot;HOST&quot;</span>: <span class="hljs-string">&quot;0.0.0.0&quot;</span>,  <span class="hljs-comment"># &#x76D1;&#x542C;ip, 0.0.0.0 &#x76D1;&#x542C;&#x6240;&#x6709;IP</span>
  <span class="hljs-string">&quot;PORT&quot;</span>: <span class="hljs-number">5010</span>        <span class="hljs-comment"># &#x76D1;&#x542C;&#x7AEF;&#x53E3;</span>
}

<span class="hljs-comment"># &#x4E0A;&#x9762;&#x914D;&#x7F6E;&#x542F;&#x52A8;&#x540E;&#xFF0C;&#x4EE3;&#x7406;&#x6C60;&#x8BBF;&#x95EE;&#x5730;&#x5740;&#x4E3A; http://127.0.0.1:5010</span>
</code></pre>
<blockquote>
<p>&#x5173;&#x4E8E;&#x914D;&#x7F6E;&#x66F4;&#x591A;&#x8BE6;&#x60C5;,&#x8BF7;&#x76F4;&#x63A5;&#x53C2;&#x8003;&#x9879;&#x76EE;&#x5B98;&#x65B9;&#x4ECB;&#x7ECD;,&#x4EE5;&#x4E0A;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x57FA;&#x672C;&#x591F;&#x7528;&#x4E86;.</p>
</blockquote>
</li>
<li><p>&#x6B65;&#x9AA4; 5 : &#x542F;&#x52A8;&#x9879;&#x76EE;</p>
<pre><code class="lang-bash"><span class="hljs-comment"># &#x5982;&#x679C;&#x4F60;&#x7684;&#x4F9D;&#x8D56;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x5B8C;&#x6210;&#x5E76;&#x4E14;&#x5177;&#x5907;&#x8FD0;&#x884C;&#x6761;&#x4EF6;,&#x53EF;&#x4EE5;&#x5728;cli&#x76EE;&#x5F55;&#x4E0B;&#x901A;&#x8FC7;ProxyPool.py&#x542F;&#x52A8;.</span>
<span class="hljs-comment"># &#x7A0B;&#x5E8F;&#x5206;&#x4E3A;: schedule &#x8C03;&#x5EA6;&#x7A0B;&#x5E8F; &#x548C; webserver Api&#x670D;&#x52A1;</span>

<span class="hljs-comment"># &#x9996;&#x5148;&#x542F;&#x52A8;&#x8C03;&#x5EA6;&#x7A0B;&#x5E8F;</span>
python proxyPool.py schedule

<span class="hljs-comment"># &#x7136;&#x540E;&#x542F;&#x52A8;webApi&#x670D;&#x52A1;</span>
python proxyPool.py webserver
</code></pre>
<blockquote>
<p>&#x8BE5;&#x547D;&#x4EE4;&#x8981;&#x6C42;&#x5F53;&#x524D;&#x73AF;&#x5883;&#x5904;&#x4E8E; <code>cli</code> &#x76EE;&#x5F55;,&#x5982;&#x679C;&#x662F;&#x5176;&#x4ED6;&#x76EE;&#x5F55;&#x8BF7;&#x81EA;&#x884C;&#x8C03;&#x6574; <code>proxyPool.py</code> &#x7684;&#x8DEF;&#x5F84;(<code>cd cli</code>)</p>
</blockquote>
</li>
</ul>
</li>
</ul>
<h3 id="&#x539F;&#x751F;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;-urllib"><a name="&#x539F;&#x751F;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;-urllib" class="anchor-navigation-ex-anchor" href="#&#x539F;&#x751F;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;-urllib"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.4. &#x539F;&#x751F;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42; urllib</h3>
<blockquote>
<p><code>urllib.urlopen(url[,data[,proxies]])</code> : <a href="https://docs.python.org/2/library/urllib.html" target="_blank">https://docs.python.org/2/library/urllib.html</a></p>
</blockquote>
<ul>
<li><code>GET</code> &#x8BF7;&#x6C42;</li>
</ul>
<blockquote>
<p>&#x5982;&#x679C;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#x7684;&#x8BDD;,&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x6784;&#x5EFA;&#x8BF7;&#x6C42; <code>URL</code>,&#x540C;&#x65F6;&#x53EF;&#x4EE5;&#x914D;&#x5408; <code>urllib.urlencode(dict)</code> &#x5E8F;&#x5217;&#x5316;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x5B57;&#x5178;.</p>
</blockquote>
<p>&#x5F53;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x4E0D;&#x592A;&#x590D;&#x6742;&#x65F6;,&#x5C24;&#x5176;&#x662F;&#x4E0D;&#x9700;&#x8981;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x65F6;,&#x53EF;&#x4EE5;&#x76F4;&#x63A5; <code>urllib2.urlopen(url)</code> &#x53D1;&#x9001;&#x8BF7;&#x6C42;,&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_simple_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5934;&#x548C;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use simple urllib2&lt;&lt;&lt;&apos;</span>
    use_simple_urllib2()
</code></pre>
<p>&#x5F53;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x6BD4;&#x8F83;&#x591A;&#x6216;&#x8005;&#x9700;&#x8981;&#x52A8;&#x6001;&#x62FC;&#x63A5;&#x65F6;,&#x63A8;&#x8350;&#x4F7F;&#x7528; <code>urllib.urlencode(dict)</code> &#x5E8F;&#x5217;&#x5316;&#x67E5;&#x8BE2;&#x53C2;&#x6570;,&#x7136;&#x540E;&#x518D;&#x62FC;&#x63A5;&#x5230;&#x8BF7;&#x6C42; <code>URL</code> &#x540E;,&#x6700;&#x7EC8;&#x5F62;&#x6210;&#x5B8C;&#x6210;&#x7684;&#x8BF7;&#x6C42; <code>URL</code>.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_params_urllib2</span><span class="hljs-params">()</span>:</span>
    params = urllib.urlencode({
        <span class="hljs-string">&apos;param1&apos;</span>: <span class="hljs-string">&apos;hello&apos;</span>, 
        <span class="hljs-string">&apos;param2&apos;</span>: <span class="hljs-string">&apos;world&apos;</span>,
        <span class="hljs-string">&apos;author&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;website&apos;</span>:<span class="hljs-string">&apos;http://blog.snowdreams1006.cn&apos;</span>,
        <span class="hljs-string">&apos;url&apos;</span>:<span class="hljs-string">&apos;https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html&apos;</span>,
        <span class="hljs-string">&apos;wechat&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;email&apos;</span>:<span class="hljs-string">&apos;snowdreams1006@163.com&apos;</span>,
        <span class="hljs-string">&apos;github&apos;</span>:<span class="hljs-string">&apos;https://github.com/snowdreams1006/&apos;</span>
    })
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/get?%s&apos;</span> % params)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Use params urllib2&lt;&lt;&lt;&apos;</span>
    use_params_urllib2()
</code></pre>
<blockquote>
<p><code>urllib2.urlopen(&apos;http://httpbin.snowdreams1006.cn/get&apos;)</code></p>
</blockquote>
<ul>
<li><code>POST</code> &#x8BF7;&#x6C42;</li>
</ul>
<blockquote>
<p>&#x76F8;&#x6BD4;&#x4E8E;&#x9ED8;&#x8BA4;&#x7684; <code>GET</code> &#x8BF7;&#x6C42;&#x65B9;&#x5F0F;,&#x53EA;&#x9700;&#x8981;&#x5C06;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#x4E0D;&#x518D;&#x62FC;&#x63A5;&#x5230;&#x8BF7;&#x6C42;&#x94FE;&#x63A5; <code>URL</code> &#x800C;&#x662F;&#x4F5C;&#x4E3A;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x53C2;&#x6570; <code>data</code>,&#x5F62;&#x5982; <code>urllib.urlopen(url,data)</code> &#x7684;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x662F; <code>POST</code> &#x8BF7;&#x6C42;.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post_params_urllib2</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5934;&#x548C;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    params = urllib.urlencode({
        <span class="hljs-string">&apos;param1&apos;</span>: <span class="hljs-string">&apos;hello&apos;</span>, 
        <span class="hljs-string">&apos;param2&apos;</span>: <span class="hljs-string">&apos;world&apos;</span>,
        <span class="hljs-string">&apos;author&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;website&apos;</span>:<span class="hljs-string">&apos;http://blog.snowdreams1006.cn&apos;</span>,
        <span class="hljs-string">&apos;url&apos;</span>:<span class="hljs-string">&apos;https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html&apos;</span>,
        <span class="hljs-string">&apos;wechat&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;email&apos;</span>:<span class="hljs-string">&apos;snowdreams1006@163.com&apos;</span>,
        <span class="hljs-string">&apos;github&apos;</span>:<span class="hljs-string">&apos;https://github.com/snowdreams1006/&apos;</span>
    })
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/post&apos;</span>,params)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Headers:&apos;</span>)
    print(response.info())
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Response Body:&apos;</span>)
    print(response.read())

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Post params urllib2&lt;&lt;&lt;&apos;</span>
    post_params_urllib2()
</code></pre>
<ul>
<li>&#x8BBE;&#x7F6E;&#x4EE3;&#x7406;</li>
</ul>
<blockquote>
<p>&#x5F53;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x6709;&#x6548;&#x65F6; <code>urllib.FancyURLopener(proxy)</code> &#x53EF;&#x53D1;&#x9001;&#x4EE3;&#x7406;&#x8BF7;&#x6C42;,&#x82E5;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x662F;&#x7A7A;&#x5B57;&#x5178;&#x65F6;&#x5219;&#x662F;&#x6E05;&#x9664;&#x4EE3;&#x7406;&#x8BBE;&#x7F6E;.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2
<span class="hljs-keyword">import</span> json

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_proxy</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://proxyip.snowdreams1006.cn/get/&apos;</span>)
    result = response.read()
    <span class="hljs-keyword">return</span> json.loads(result)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_proxy_urllib</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x901A;&#x8FC7;&#x4EE3;&#x7406;&#x53D1;&#x9001;&#x8BF7;&#x6C42;
    &apos;&apos;&apos;</span>
    <span class="hljs-comment"># &#x968F;&#x673A;&#x4EE3;&#x7406; ip</span>
    ip = get_proxy().get(<span class="hljs-string">&apos;proxy&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Get Proxy:&apos;</span>)
    print(ip)
    proxy = {
        <span class="hljs-string">&apos;http&apos;</span>: <span class="hljs-string">&apos;http://{}&apos;</span>.format(ip),
        <span class="hljs-string">&apos;https&apos;</span>: <span class="hljs-string">&apos;https://{}&apos;</span>.format(ip)
    }
    opener = urllib.FancyURLopener(proxy)
    response = opener.open(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/ip&apos;</span>)
    result = response.read()
    result = json.loads(result)
    response_ip = result.get(<span class="hljs-string">&apos;origin&apos;</span>)
    proxy_ip = ip.split(<span class="hljs-string">&apos;:&apos;</span>)[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">if</span> proxy_ip == response_ip:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Proxy Success&apos;</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Proxy Fail&apos;</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Get proxy urllib&lt;&lt;&lt;&apos;</span>
    get_proxy_urllib()
</code></pre>
<p>&#x9664;&#x4E86;&#x4F7F;&#x7528; <code>urllib.FancyURLopener(proxy)</code> &#x8BBE;&#x7F6E;&#x4EE3;&#x7406;&#x8BF7;&#x6C42;&#x5916;,&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>urllib2.urlopen(url,data,proxies)</code> &#x53D1;&#x9001; <code>GET</code> &#x6216; <code>POST</code> &#x8BF7;&#x6C42;&#x7684;&#x4EE3;&#x7406;&#x8BF7;&#x6C42;.</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> urllib
<span class="hljs-keyword">import</span> urllib2
<span class="hljs-keyword">import</span> json

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_proxy</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x83B7;&#x53D6;&#x968F;&#x673A;&#x4EE3;&#x7406;
    &apos;&apos;&apos;</span>
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://proxyip.snowdreams1006.cn/get/&apos;</span>)
    result = response.read()
    <span class="hljs-keyword">return</span> json.loads(result)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post_proxy_urllib</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    &#x901A;&#x8FC7;&#x4EE3;&#x7406;&#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x5934;&#x548C;&#x54CD;&#x5E94;&#x4F53;&#x4FE1;&#x606F;
    &apos;&apos;&apos;</span>
    data = urllib.urlencode({
        <span class="hljs-string">&apos;param1&apos;</span>: <span class="hljs-string">&apos;hello&apos;</span>, 
        <span class="hljs-string">&apos;param2&apos;</span>: <span class="hljs-string">&apos;world&apos;</span>,
        <span class="hljs-string">&apos;author&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;website&apos;</span>:<span class="hljs-string">&apos;http://blog.snowdreams1006.cn&apos;</span>,
        <span class="hljs-string">&apos;url&apos;</span>:<span class="hljs-string">&apos;https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html&apos;</span>,
        <span class="hljs-string">&apos;wechat&apos;</span>:<span class="hljs-string">&apos;snowdreams1006&apos;</span>,
        <span class="hljs-string">&apos;email&apos;</span>:<span class="hljs-string">&apos;snowdreams1006@163.com&apos;</span>,
        <span class="hljs-string">&apos;github&apos;</span>:<span class="hljs-string">&apos;https://github.com/snowdreams1006/&apos;</span>
    })
    ip = get_proxy().get(<span class="hljs-string">&apos;proxy&apos;</span>)
    print(<span class="hljs-string">&apos;&gt;&gt;&gt;Get Proxy:&apos;</span>)
    print(ip)
    proxies = {
        <span class="hljs-string">&apos;http&apos;</span>: <span class="hljs-string">&apos;http://{}&apos;</span>.format(ip),
        <span class="hljs-string">&apos;https&apos;</span>: <span class="hljs-string">&apos;https://{}&apos;</span>.format(ip)
    }
    response = urllib2.urlopen(<span class="hljs-string">&apos;http://httpbin.snowdreams1006.cn/post&apos;</span>,data=data,proxies=proxies)
    result = response.read()
    result = json.loads(result)
    response_ip = result.get(<span class="hljs-string">&apos;origin&apos;</span>)
    proxy_ip = ip.split(<span class="hljs-string">&apos;:&apos;</span>)[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">if</span> proxy_ip == response_ip:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Proxy Success&apos;</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;Proxy Fail&apos;</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&gt;&gt;&gt;Get proxy urllib&lt;&lt;&lt;&apos;</span>
    post_proxy_urllib()
</code></pre>
<p><code>python2</code> &#x7684; <code>urllib.urlopen(url[,data[,proxies]])</code> &#x7684;&#x76F8;&#x5173;&#x6F14;&#x793A;&#x57FA;&#x672C;&#x4E0A;&#x5168;&#x90E8;&#x8986;&#x76D6;&#x5B8C;&#x6BD5;,&#x63A8;&#x8350;&#x8BFB;&#x8005;&#x5B9E;&#x9645;&#x7EC3;&#x4E60;&#x4E00;&#x4E0B;.</p>
<p><strong>&#x4E0B;&#x8282;&#x9884;&#x544A;</strong>:</p>
<p>&#x8BBF;&#x95EE;<a href="https://api.github.com/" target="_blank">https://api.github.com/</a>&#x8BF7;&#x6C42;&#x611F;&#x5174;&#x8DA3;&#x7684;&#x63A5;&#x53E3;,&#x4EB2;&#x6D4B;&#x516C;&#x5F00;&#x6570;&#x636E;.</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;current_user_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/user&quot;</span>,
  <span class="hljs-string">&quot;current_user_authorizations_html_url&quot;</span>: <span class="hljs-string">&quot;https://github.com/settings/connections/applications{/client_id}&quot;</span>,
  <span class="hljs-string">&quot;authorizations_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/authorizations&quot;</span>,
  <span class="hljs-string">&quot;code_search_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/search/code?q={query}{&amp;page,per_page,sort,order}&quot;</span>,
  <span class="hljs-string">&quot;commit_search_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/search/commits?q={query}{&amp;page,per_page,sort,order}&quot;</span>,
  <span class="hljs-string">&quot;emails_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/user/emails&quot;</span>,
  <span class="hljs-string">&quot;emojis_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/emojis&quot;</span>,
  <span class="hljs-string">&quot;events_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/events&quot;</span>,
  <span class="hljs-string">&quot;feeds_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/feeds&quot;</span>,
  <span class="hljs-string">&quot;followers_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/user/followers&quot;</span>,
  <span class="hljs-string">&quot;following_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/user/following{/target}&quot;</span>,
  <span class="hljs-string">&quot;gists_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/gists{/gist_id}&quot;</span>,
  <span class="hljs-string">&quot;hub_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/hub&quot;</span>,
  <span class="hljs-string">&quot;issue_search_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/search/issues?q={query}{&amp;page,per_page,sort,order}&quot;</span>,
  <span class="hljs-string">&quot;issues_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/issues&quot;</span>,
  <span class="hljs-string">&quot;keys_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/user/keys&quot;</span>,
  <span class="hljs-string">&quot;label_search_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/search/labels?q={query}&amp;repository_id={repository_id}{&amp;page,per_page}&quot;</span>,
  <span class="hljs-string">&quot;notifications_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/notifications&quot;</span>,
  <span class="hljs-string">&quot;organization_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/orgs/{org}&quot;</span>,
  <span class="hljs-string">&quot;organization_repositories_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}&quot;</span>,
  <span class="hljs-string">&quot;organization_teams_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/orgs/{org}/teams&quot;</span>,
  <span class="hljs-string">&quot;public_gists_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/gists/public&quot;</span>,
  <span class="hljs-string">&quot;rate_limit_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/rate_limit&quot;</span>,
  <span class="hljs-string">&quot;repository_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/repos/{owner}/{repo}&quot;</span>,
  <span class="hljs-string">&quot;repository_search_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/search/repositories?q={query}{&amp;page,per_page,sort,order}&quot;</span>,
  <span class="hljs-string">&quot;current_user_repositories_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/user/repos{?type,page,per_page,sort}&quot;</span>,
  <span class="hljs-string">&quot;starred_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/user/starred{/owner}{/repo}&quot;</span>,
  <span class="hljs-string">&quot;starred_gists_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/gists/starred&quot;</span>,
  <span class="hljs-string">&quot;user_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/users/{user}&quot;</span>,
  <span class="hljs-string">&quot;user_organizations_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/user/orgs&quot;</span>,
  <span class="hljs-string">&quot;user_repositories_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/users/{user}/repos{?type,page,per_page,sort}&quot;</span>,
  <span class="hljs-string">&quot;user_search_url&quot;</span>: <span class="hljs-string">&quot;https://api.github.com/search/users?q={query}{&amp;page,per_page,sort,order}&quot;</span>
}
</code></pre>
<h2 id="&#x53C2;&#x8003;&#x6587;&#x6863;"><a name="&#x53C2;&#x8003;&#x6587;&#x6863;" class="anchor-navigation-ex-anchor" href="#&#x53C2;&#x8003;&#x6587;&#x6863;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. &#x53C2;&#x8003;&#x6587;&#x6863;</h2>
<ul>
<li><a href="https://www.cnblogs.com/yun1108/p/8967334.html" target="_blank">Python&#x4E2D;read()&#x3001;readline()&#x548C;readlines()&#x4E09;&#x8005;&#x95F4;&#x7684;&#x533A;&#x522B;&#x548C;&#x7528;&#x6CD5;</a></li>
<li><a href="https://www.cnblogs.com/sysu-blackbear/p/3629420.html" target="_blank">Python&#x6838;&#x5FC3;&#x6A21;&#x5757;&#x2014;&#x2014;urllib&#x6A21;&#x5757;</a></li>
<li><a href="https://www.jianshu.com/p/260f18aa5462" target="_blank">Gunicorn&#x8FD0;&#x884C;&#x4E0E;&#x914D;&#x7F6E;</a></li>
<li><a href="https://blog.csdn.net/momoyaoquaoaoao/article/details/93863115" target="_blank">Gunicorn&#x5E38;&#x7528;&#x914D;&#x7F6E;</a></li>
</ul>
<pre><code class="lang-html">&#x4F5C;&#x8005;: &#x96EA;&#x4E4B;&#x68A6;&#x6280;&#x672F;&#x9A7F;&#x7AD9;
&#x94FE;&#x63A5;: https://snowdreams1006.github.io/learn-python/url/urllib/teaching.html
&#x6765;&#x6E90;: &#x96EA;&#x4E4B;&#x68A6;&#x6280;&#x672F;&#x9A7F;&#x7AD9;
&#x672C;&#x6587;&#x539F;&#x521B;&#x53D1;&#x5E03;&#x4E8E;&#x300C;&#x96EA;&#x4E4B;&#x68A6;&#x6280;&#x672F;&#x9A7F;&#x7AD9;&#x300D;,&#x8F6C;&#x8F7D;&#x8BF7;&#x6CE8;&#x660E;&#x51FA;&#x5904;,&#x8C22;&#x8C22;&#x5408;&#x4F5C;!
</code></pre>
<p><img src="https://snowdreams1006.github.io/snowdreams1006-wechat-open.png" alt="https://snowdreams1006.github.io/snowdreams1006-wechat-open.png"></p>
<div id="gitalk-container"></div>
                </div>
                <script src="https://my.openwrite.cn/js/readmore.js"></script>
                <script>
                    var enablePlugin = false;
                    var allowDomain = "localhost";
                    if(allowDomain){
                        var currentDomain = location.hostname;
                        if ($.isArray(allowDomain)) {
                            $.each(allowDomain, function(index, item) {
                                if (currentDomain == item) {
                                    enablePlugin = true;
                                    return false;
                                }
                            });
                        }else{
                           if (currentDomain == allowDomain) {
                                enablePlugin = true;
                            }
                        }
                    }else{
                        enablePlugin = true;
                    }
                    
                    if(enablePlugin){
                        var isMobile = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
                        if (!isMobile) {
                            var btw = new BTWPlugin();
                            btw.init({"allowDomain":"localhost","blogId":"15702-1569305559839-744","name":"雪之梦技术驿站","qrcode":"https://snowdreams1006.github.io/snowdreams1006-wechat-public.jpeg","keyword":"vip","id":"vip-container"});
                        }
                    }
                </script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="about.html" class="navigation navigation-prev " aria-label="Previous page: 原生请求">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="exercise.html" class="navigation navigation-next " aria-label="Next page: 实例练习">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"范例教学","level":"1.4.1.1","depth":3,"next":{"title":"实例练习","level":"1.4.1.2","depth":3,"path":"url/urllib/exercise.md","ref":"url/urllib/exercise.md","articles":[]},"previous":{"title":"原生请求","level":"1.4.1","depth":2,"path":"url/urllib/about.md","ref":"url/urllib/about.md","articles":[{"title":"范例教学","level":"1.4.1.1","depth":3,"path":"url/urllib/teaching.md","ref":"url/urllib/teaching.md","articles":[]},{"title":"实例练习","level":"1.4.1.2","depth":3,"path":"url/urllib/exercise.md","ref":"url/urllib/exercise.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","copy-code-button","favicon-absolute","github-buttons","edit-link-plus","readmore","copyright","mygitalk","icp"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"copyright":{"site":"https://snowdreams1006.github.io/learn-python","author":"雪之梦技术驿站","website":"雪之梦技术驿站","image":"https://snowdreams1006.github.io/snowdreams1006-wechat-open.png"},"search":{},"edit-link-plus":{"label":"Edit This Page","base":{"snowdreams1006.github.io":"https://github.com/snowdreams1006/learn-python/edit/master/docs","snowdreams1006.gitlab.io":"https://gitlab.com/snowdreams1006/learn-python/edit/master/docs","snowdreams1006.gitee.io":"https://gitee.com/snowdreams1006/learn-python/edit/master/docs"},"defaultBase":"https://github.com/snowdreams1006/learn-python/edit/master/docs"},"readmore":{"allowDomain":"localhost","blogId":"15702-1569305559839-744","name":"雪之梦技术驿站","qrcode":"https://snowdreams1006.github.io/snowdreams1006-wechat-public.jpeg","keyword":"vip"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"favicon-absolute":{"appleTouchIconMore":{"120x120":"/apple-touch-icon-120.png","180x180":"/apple-touch-icon-180.png"},"appleTouchIconPrecomposed152":"/apple-touch-icon-precomposed-152.png","appleTouchIconPrecomposedMore":{"120x120":"/apple-touch-icon-precomposed-120.png","180x180":"/apple-touch-icon-precomposed-180.png"},"favicon":"/favicon.ico","bookmark":"/bookmark.ico","appleTouchIcon152":"/apple-touch-icon-152.png"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"icp":{"link":"http://www.beian.miit.gov.cn/","number":"浙ICP备18042346号"},"github-buttons":{"buttons":[{"user":"snowdreams1006","repo":"learn-python","type":"star","size":"small"}]},"copy-code-button":{},"mygitalk":{"flipMoveOptions":{},"clientID":"3f62415a283d19cbd696","number":-1,"perPage":10,"proxy":"https://cors-anywhere.herokuapp.com/https://github.com/login/oauth/access_token","admin":["snowdreams1006"],"createIssueManually":false,"distractionFreeMode":false,"repo":"learn-python","owner":"snowdreams1006","enableHotKey":true,"clientSecret":"aed0e1db0620bf5d0e3a3f0225f801997ad74e58","pagerDirection":"last","labels":["Gitalk"]},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"snowdreams1006","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"python 学习笔记","language":"zh-hans","links":{"sidebar":{"blog":"https://snowdreams1006.tech/"}},"gitbook":"3.2.3","description":"python 学习笔记,python 入门教程"},"file":{"path":"url/urllib/teaching.md","mtime":"2020-01-18T08:58:18.957Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-01-18T09:06:08.548Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-edit-link-plus/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-copyright/copyright.js"></script>
        
    
        
        <script src="https://cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>
        
    
        
        <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-mygitalk/mygitalk.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

